I have stack overflow...

Is this not a proper delete function for a dynamic 2d array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    FloodMap::~FloodMap(){
    
    for (int i = 0; i < rows; i++) {

        delete[] map[i];

    }

    delete[] map;

    cout << "Dynamic deletion complete" << endl;

}

//It's a struct array inside a class...

/*
struct pixel{
    int r;
    int g;
    int b;
};

class FloodMap{
private:
    pixel **map;
*/


Thank you.
looks fine. look for out of bounds access elsewhere, or if you really think it is here, validate 'rows' value.
Rows was the value it needed to be, thanks for the tip.

The reason why I think the problem lies in that function is because I never see the cout for Dynamic Deletion Complete.
closed account (z05DSL3A)
Did you create rows or columns first?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main ()
{
    int x{ 10 }, y{ 20 };
    //
    int** test = new int* [x];
    for (int i = 0; i < x; i++)
    {
        test[i] = new int[y];
        for (int j = 0; j < y; j++)
        {
            test[i][j] = 1;
        }
    }
    //
    for (int i = 0; i < x; i++)
    {
        delete[] test[i];
    }
    delete[] test;

    return 0;
}
Last edited on
I initialized it with rows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
FloodMap::FloodMap(string infile){
    ifstream file;

    file.open(infile.c_str());

    file >> rows;
    file >> cols;
    file.get();

    map = new pixel*[rows];

    for (int i = 0; i < cols; i++) {

        map[i] = new pixel[cols];

    }

    for(int i = 0; i < rows; i++){

        for(int j = 0; j < cols; j++){

            file >> map[i][j].r;
            map[i][j].b = map[i][j].r;
            map[i][j].g = map[i][j].r;

        }

    }

    file.close();

}
closed account (z05DSL3A)
Is line 12 correct? Should it be for (int i = 0; i < rows; i++) {?
There are more cols than rows. Wouldn't it stop grabbing cols when it reach the max number of rows?
If it helps, here's a scaler function to get all the values read from file into a range of 0-255.
Maybe I messed up here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void FloodMap::scaleData(){

    int old_max = map[0][0].r;
    int old_min = map[0][0].r;
    double old_range = 0;


    for(int i = 0; i < rows; i++) {

        for (int j = 0; j < cols; j++) {

            if (map[i][j].r < old_min) {
                old_min = map[i][j].r;
            }

            if (map[i][j].r > old_max) {
                old_max = map[i][j].r;
            }

        }
    }

    old_range = (old_max - old_min);

    for(int i = 0; i < rows; i++) {

        for (int j = 0; j < cols; j++) {
            int val = ((map[i][j].r - old_min) / old_range) * 255;
            map[i][j].r = val;
            map[i][j].g = val;
            map[i][j].b = val;

        }
    }
}
closed account (z05DSL3A)
There are more cols than rows. Wouldn't it stop grabbing cols when it reach the max number of rows?

No it won't stop until it reaches the limit of the for loop. So you would be creating cols number of cols...
>> No it wont stop until....

I changed it, and it gave me a nicer flooding and still created an image, so it was definitely better code, however I still am not reaching the cout statement in my destructor and still have the overflow.

Thanks for the more efficient snippet though.
If there a reason why you can't use a 2D std::vector instead of a 2D C-style newed array?

The STL is designed to be less of a micro-management issue. Memory management is built into the class.

Creating a variable sized array with std::vector, even if it is 2D, is easy to do at run-time. And once created the elements can be accessed using operator[], the same as element access is done with the C-style array.

A std::vector has the added advantage of being able to keep track of its size, a very useful feature when passed into a function.
There is indeed a reason. Use of extra libraries results in a deduction.
So....you don't want to use well-written constructs that were designed to help prevent the myriad of problems you are encountering, and will likely run across in the future.

That, of course, is your choice.
I was trying to be tactful, but you didn't get my meaning.

It is not my choice. Vectors are not allowed in this project.
It is not my choice. Vectors are not allowed in this project.

If you had said that from the start I would have understood and left it alone after my question and first comment.

Carry on.
.>>GreyWolf

Your solution was absolute. I forgot to change back an earlier tinker that I had made messing around with it. Once I did, it was fixed.

Thanks man.
Topic archived. No new replies allowed.