Delete a dynamically allocated 2d array of bool

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xunter
    New Member
    • Nov 2011
    • 7

    Delete a dynamically allocated 2d array of bool

    I have the code that goes bellow. I use Visual Studio and try to debug my program.

    Code:
    ...
    int _row;
    int _col;
    bool **_matrixArr;
    ...
    
    
    BinaryMatrix::~BinaryMatrix() {
    	for (int i = 0; i < _row; i++) {
    		delete [] _matrixArr[i];
    	}
    	delete [] _matrixArr;
    };
    
    void BinaryMatrix::InitMatrixArray() {	
    	bool **matrix = new bool*[_row];
    	for (int i = 0; i < _row; i++) {
    		matrix[i] = new bool[_col];
    	}
    	_matrixArr = matrix;
    };
    During a program run I get an error: Debug Error! HEAP CORRUPTION DETECTED... CRT detected that the application wrote to memory after end of heap buffer.

    What is wrong?

    Thanks.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The code you posted looks fine. From that the only error that could occur would happen if you constructed a BinaryMatrix and then destroyed it without calling InitMatrixArray since the destructor assumes it has been allocated.

    Comment

    • xunter
      New Member
      • Nov 2011
      • 7

      #3
      Originally posted by Banfa
      The code you posted looks fine. From that the only error that could occur would happen if you constructed a BinaryMatrix and then destroyed it without calling InitMatrixArray since the destructor assumes it has been allocated.
      Look at the ctor:

      Code:
      BinaryMatrix::BinaryMatrix(int rowSize, int colSize) {
      	_row = rowSize;
      	_col = colSize;
      
      	_matrixArr = null;
      
      	InitMatrixArray();
      };
      I use this class like below

      Code:
      BinaryMatrix *bm = new BinaryMatrix(nRow, nCol);
      ...
      delete bm;
      I am a newbie in c/c++ and I can make a stupid mistake... But I don't know where do I need looking for...

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Read this: http://bytes.com/topic/c/insights/77...rrays-revealed

        The code posted is way too complicated. Your array can be declared in one line of code.

        Also, a bool** is not a pointer to s 2D array. It's just a pointer to a bool* and a bool* is just a pointer to a single bool.

        Comment

        • xunter
          New Member
          • Nov 2011
          • 7

          #5
          OK, thanks all. I have located a problem in my code. There was the index out of bounds error in some code in which I read\write items outside the array range.

          Now, I change the bool** to bool* and also I allocate memory by malloc and access 2d array items by row * colCount + col. And, finally, I cleen the allocated memory in the destructor by the free function. But I guess that the previous code also will work properly.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Your code is not allocating a 2D array.

            It is allocating an array of bool* and then it allocates another separate array of bool for each element of the first array.

            A 2D array is a single array.

            Comment

            Working...