1

I am using 2D pointer matrix in C++ (using visual studio 2015). I created the matrix and deleted it after done.

typedef unsigned char  U8;
typedef unsigned int   U32;
U8** A0 = new U8*[M0];
for (U32 i = 0; i < M0; ++i)
{
    A0[i] = new U8[m_L0];
}
for (U32 i = 0; i < M0; ++i){
    if (i < m_L0)
    {
        delete[] A0[i];
        A0[i] = NULL;
    }
}
delete[] A0;
A0 = NULL;

However, I got a error as the bellow figure. Do I need to check matrix not NULL before delete? enter image description here

Sometime, it crashed as the figure enter image description here

What is my happen? Could you help me to fix it? This is my full source code

#include <iostream>
#include <stdlib.h>
#include <time.h>       /* time */
#define random(x) (rand()%x)
typedef unsigned char  U8;
typedef unsigned int   U32;
class CData
{
private:
    U8* m_Data;
    U32  m_Len;
public:
    CData(void) : m_Data(NULL), m_Len(0)
    {
    }
    ~CData(void)
    {
        FreeData();
    }
    void FreeData()
    {
        if (m_Data)
        {
            delete[] m_Data;
            m_Data = NULL;
        }
    }
};
void create_and_delete_matrix()
{
    U32 M0;
    U32 m_L0;
    U32 range = 20;
    M0   = random(range);
    m_L0 = random(range);
    U8** A0 = new U8*[M0];
    for (U32 i = 0; i < M0; ++i)
    {
        A0[i] = new U8[m_L0];
    }
    for (U32 i = 0; i < M0; ++i) {
        for (U32 j = 0; j < m_L0; ++j)  {
            A0[i][j] = random(2);
            printf("%d ", A0[i][j]);
        }
        printf("\n");
    }
    CData** D0 = new CData*[M0];
    for (U32 i = 0; i < M0; ++i){
        D0[i] = new CData();
    }

    //free A0, C0, D0 matrix
    for (U32 i = 0; i < M0; ++i){
        if (i < m_L0)
        {
            delete[] A0[i];
            A0[i] = NULL;
        }
        delete D0[i];
        D0[i] = NULL;
    }
    delete[] A0;
    A0 = NULL;

    delete[] D0;
    D0 = NULL;

}
int main(int argc, char **argv) {
    unsigned int time_ui = static_cast<unsigned int>(time(NULL));
    srand(time_ui);
    for (U32 iter = 0; iter < 100; iter++){
        create_and_delete_matrix();
    }
    return 0;
}
Jame
  • 3,746
  • 6
  • 52
  • 101

2 Answers2

1

Your code looks mostly OK to me. The only place there could be a problem is in the lines:

M0   = random(range);
m_L0 = random(range);

With that, it is possible for M0 and/or m_L0 to be zero. I would suggest changing them to:

M0   = random(range) + 1;
m_L0 = random(range) + 1;

or add special logic to account for the cases where one of them is set to zero.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Why should 0 be a problem? Do you see any case in the code in which it doesn't do the right thing? – 6502 Dec 28 '15 at 06:49
  • @6502, I couldn't find anything in the standard that indicates whether passing `0` to `operator new []` is OK or not. – R Sahu Dec 28 '15 at 06:52
  • @6502, thanks for the link. Based on that, calling `operator new []` with `0` as argument should be OK. – R Sahu Dec 28 '15 at 06:58
0

The code seems ok.

May be you defined (or you are using) a custom global memory allocator for objects or for arrays that doesn't handle correctly requests of 0 bytes?

6502
  • 112,025
  • 15
  • 165
  • 265