Search code examples
c++pointersmemory-managementdynamic-arrays

Dynamically allocated arrays and heap corruption


I'm a newbie programmer wrestling with pointers and dynamic array allocation.

For the program I'm working on, I need to allocate a 2-D using user input for the dimensions and the values at each index.

I later de-allocate the array using delete[] and get a heap corruption error.

here is my allocation:

    cin>>w>>h;

    int** pond;

    //allocating array
    pond = new int*[w];

    for(int j = 0; j<w; j++)
    {
        pond[j] = new int[h];
    }



    // inputting array values
    for(int k = 0; k < h; k++)
    {
        for(int l = 0; l < w; l++)
        {
            cin>>entry;
            pond[l][w] = entry;
            cout<<"entry is: "<< pond[l][w]<<endl;
        }
    }
    cout<<pond[0][0]<<endl;

the cout statement at the end is to attempt to verify a value in the array. The problem is, it gives something like -8023940... a random number not even close to the user input, which is simple integers(1-9). However, the cout statement inside the for loop produces the correct value.

My question is, what am I doing wrong here? the allocation seems simple enough, but the values are crazy as soon as it steps out of that for loop.

Here is my entire program:

#include <iostream>
#include <string>
using namespace std;


int main()
{

int trials = 0;
int w = 0;
int h = 0;
int entry = 0;
int fishMax = 0;
int holdFishRow = 0;
int holdFishCol = 0;
int fishTotal = 0;
int highRow = 0;
int highCol = 0;

char done = 's';


cin>>trials;

for(int i = 0; i < trials; i++)
{
    cin>>w>>h;

    int** pond;

    //allocating array
    pond = new int*[w];

    for(int j = 0; j<w; j++)
    {
        pond[j] = new int[h];
    }



    // inputting array values
    for(int k = 0; k < h; k++)
    {
        for(int l = 0; l < w; l++)
        {
            cin>>entry;
            pond[l][w] = entry;
            cout<<"entry is: "<< pond[l][w]<<endl;
        }
    }
    cout<<pond[0][0]<<endl;


    // "fishing"...bombing each array index bomberman style.
    for(int z = 0; z < h; z++)
    {
        for(int j = 0; j < w; j++)
        {
            for(int k = 0; k < h; k++)
            {
                holdFishCol = holdFishCol + pond[j][k];
                cout<<pond[j][k]<<endl;
            }

            for(int l = 0; l < w; l++)
            {
                holdFishRow += pond[l][z];
            }

            fishTotal = holdFishCol + holdFishRow;

            if(fishTotal > fishMax)
            {
                fishMax  = fishTotal;
                highRow = j;
                highCol = z;
            }


        }
    }





    cout<< "#"<<i<<": ("<<highRow<<", "<<highCol<<") "<<fishMax<<endl;

    for(int q = 0; q < w; q++)
    {
        delete[] pond[q];
    }

    delete[] pond;

}



cin>>done;

return 0;
}

It walks through each array given, and adds the values of each index in a cross pattern(like a plus +), totals it, and gives a score for each index.


Solution

  • The problem is most likely this statement:

    pond[l][w] = entry;
    

    Unless w is less than h, then you're writing out of bounds. I suspect you mean

    pond[l][k] = entry;