Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used the following code to create a 2D dynamically allocated array:
int x,y;
    cout<<"Entre matrix dimensions"<<endl;
    cin>>x>>y;

    //Dynamically Allocating x*y array
    int **myArray = new int*[x];              //reserve x row elements
    for (int i = 0; i < x; ++i) {
        for (int j = 0; j < y; ++j) {
            myArray[i] = new int[j];           //makes each one of the reserved row elements points to an array
        }
    }

    //entreing the values
    cout<<"Entre the values"<<endl;
    for (int i = 0; i < x; ++i) {
        for (int j = 0; j < y; ++j) {
            cin>>myArray[i][j] ;
        }
    }

    //Displaying the values
    cout<<"You entred : "<<endl;
    for (int i = 0; i < x; ++i) {
        for (int j = 0; j < y; ++j) {
            cout<<myArray[i][j]<<" " ;
        }
        cout<<endl;
    }

When i tested the previous code, it worked well.But when i tried the following code to deallocate the 2d array:
 //Memory Deallocation
 for (int i = 0; i < x; ++i) {
         delete [] myArray[i];
     }
delete[] myArray;

An error occured:
Heap corruption detected. CRT detected that the application wrote to memory after end of heap buffer


What I have tried:

I have tried to just use
delete[] myArray;
for memory deallocation and the error message did not appear. But i do not know is that right or wrong?
Posted
Updated 15-Feb-18 23:05pm

That is because your delete code does not match the allocation code:
//Dynamically Allocating x*y array
int **myArray = new int*[x];              //reserve x row elements
for (int i = 0; i < x; ++i) {
    for (int j = 0; j < y; ++j) {
        myArray[i] = new int[j];           //makes each one of the reserved row elements points to an array
    }
}
//Memory Deallocation
for (int i = 0; i < x; ++i) {
    delete [] myArray[i];
}
delete[] myArray;
For each x you are allocating y arrays which are all assigned to myArray[i]. So only the last allocated one will be used (and later freed). Also this last one has the size of y-1 which is the reason for the heap corruption because you are accessing one more (not allocated) item.

Just use a single loop for allocation like when deleting:
for (int i = 0; i < x; ++i) {
    myArray[i] = new int[y];
}
 
Share this answer
 
Comments
Ahmed AE 16-Feb-18 7:46am    
What about using only delete[] to delete it?
Jochen Arndt 16-Feb-18 8:07am    
That will only delete myArray and not the elements which has been allocated too. It would result in a "memory leak" (having allocated memory which is not used anymore).
Ahmed AE 17-Feb-18 15:02pm    
How to test this?
Jochen Arndt 18-Feb-18 3:09am    
Just search for "c++ memory leak detectors". They must be added to your code and are executed when terminating the application.
Your delete is fine. Your memory allocation is wrong.
Try:
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
  int x,y;
  cout<<"Entre matrix dimensions"<<endl;
  cin>>x>>y;
  cout << "x = " << x << ", y = " << y << endl;


  //Dynamically Allocating x*y array
  int **myArray = new int * [x];              //reserve x row elements
  for (int i = 0; i < x; ++i)
    myArray[i] = new int[y];

  cout << "assigning random values"<< endl;
  for (int i = 0; i < x; ++i)
    for (int j = 0; j < y; ++j)
      myArray[i][j] = rand();

  //Displaying the values
  cout<<"the values : "<<endl;
  for (int i = 0; i < x; ++i)
  {
    for (int j = 0; j < y; ++j)
      cout<<myArray[i][j]<<" " ;
    cout << endl;
  }

  //Memory Deallocation
  for (int i = 0; i < x; ++i)
    delete [] myArray[i];

  delete[] myArray;
}


By the way, the standard library gently provides you the vector container, why don't you use it?
 
Share this answer
 
v2
Comments
Ahmed AE 16-Feb-18 7:00am    
because i will use these matrices in mathematical calculations. And the vector will consume more CPU and Ram resources.
CPallini 16-Feb-18 7:21am    
I would try it before claiming. What I mean is the vector overhead could be negligible.
Ahmed AE 16-Feb-18 7:35am    
OK, i will try
Ahmed AE 16-Feb-18 7:38am    
But what about using only delete[] myArray;
as array deallocation?
CPallini 16-Feb-18 8:52am    
No, you should not. Again, the deallocation in your code snippet is correct.
You have allocated the correct memory for your array, so you get this mess. You the error WHEN accessing invalid memory and NOT when deleting (the correct allocated but misused) memory block.

this code should do the job:
C++
int *myArray = new int[x * y];//allocate array of size (fixed the wrong "+" operator to "*"
delete[] myArray;//cleanup 

Tip: use memory view window to see the data and play a bit with it. ;-)
 
Share this answer
 
v3
Comments
Richard MacCutchan 16-Feb-18 4:45am    
Should be new int[x * y]; // x rows each of y columns
CPallini 16-Feb-18 5:24am    
Good catch!
Ahmed AE 16-Feb-18 6:35am    
He made me confused
KarstenK 19-Feb-18 4:22am    
s bug => I'll fix it
Usman Hunjra 19-Feb-18 6:27am    
Actually this type of allocation gives you a chunk of memory but remember in order to access the elements of the array you have to use Row, Column Major Addressing formula.
You cannot use the subscript operator.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900