3

I have a 2D pointer matrix in C++ such as,

typedef unsigned char  U8;
typedef unsigned int   U32;
int M=10;
int m_L=8;
U8** A = new U8*[M];
for (U32 i = 0; i < M; ++i)
{
    A[i] = new U8[m_L];
}

After setting value in A0, I will write a function which decide delete or not delete M-2 rows in A, depends on the random number is 0 or 1

void delete_or_not(U8** A,int M) 
{
  if (rand_num==1){
     for (U32 index = M-2; index < M; ++index){
        delete[] A[index];
     }
  }
}

Now, in main function (which contains A memory allocation), I want to free/delete the memory which allocated for A. I can use the code

//free A matrix
for (U32 i = 0; i < M; ++i)
{
    if (i < m_L)
    {
      delete[] A[i];
      A[i] = NULL;
    }
}
delete[] A;
A = NULL;

My problem is that, I don't know that A is delete (M-2) rows or not. Hence, above code does clearly delete all memory, if my random number is 0. That means, above code only delete correct memory if M-2 rows is deleted in the delete_or_not function. How can delete the A matrix perfectly. Thanks

Finaly, my full code is

typedef unsigned char  U8;
typedef unsigned int   U32;
int M=10;
int m_L=8;
U8** A = new U8*[M];
for (U32 i = 0; i < M; ++i)
{
    A[i] = new U8[m_L];
}
delete_or_not(A,M);
//free A matrix 
//Way 1: will miss M-2 row if delete_or_not function did not delete 2 rows.
// It only correct if rand_num=1
for (U32 i = 0; i < M; ++i)
{
    if (i < m_L)
    {
      delete[] A[i];
      A[i] = NULL;
    }
}
delete[] A;
A = NULL;

//Way 2- It will correct if the size of A is M by M

for (U32 i = 0; i < M; ++i)
{
      delete[] A[i];
      A[i] = NULL;
}
delete[] A;
A = NULL;
3
  • 1
    There are more readable names to chose from than U32, U8, M, m_L etc Commented Dec 31, 2015 at 7:05
  • 1
    Don't use raw arrays, pointers, new or new[], and you won't have to worry about them. Commented Dec 31, 2015 at 7:08
  • 1
    Also notice that your description and your code is not the same. Your description states: delete or not delete M-2 rows but your code actually deletes 2 elements because you have U32 index = M-2 as initialization of index in the for-loop. Commented Dec 31, 2015 at 7:43

2 Answers 2

4

Just set deleted elements to NULL and everything will work fine:

void delete_or_not(U8** A,int M) 
{
  if (rand_num==1){
     for (U32 index = M-2; index < M; ++index){
        delete[] A[index];
        A[index] = NULL;
     }
  }
}

Also, this is not very useful:

for (U32 i = 0; i < M; ++i)
{
    if (i < m_L)
    {
      delete[] A[i];
      A[i] = NULL;
    }
}

There's no point advancing i from 0 to M if you then only "do work" when i is smaller then m_L.


But really, in C++ you should probably use std::vector<std::vector<U8>> instead and simply erase or pop_back to get rid of "rows".

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, maybe I was wrong explaination. My expected is how can I delete A matrix after call the delete_or_not function. I will update
@user8430 - did you try my suggested solution?
1

In void delete_or_notyou shall set deleted elements to NULL as already proposed by another answer by Amit.

Then Way 2 of your posted code is correct in both cases.

Calling delete on a NULL is perfectly legal and doesn't hurt at all.

Way 1 is not working and should be removed.

In summary:

void delete_or_not(U8** A,int M) 
{
  if (rand_num==1){
     for (U32 index = M-2; index < M; ++index){
        delete[] A[index];
        A[index] = NULL;    // Add this to your code
     }
  }
} 


// In main somewhere...

// free A
for (U32 i = 0; i < M; ++i)
{
      delete[] A[i];   // Not a problem if the element has already been deleted
                       // because the pointer will be NULL and calling
                       // delete with NULL is OK (and changes nothing)
      A[i] = NULL;
}
delete[] A;
A = NULL;

3 Comments

Thanks. But way 2 will crashed if random_num=1. Thus, 2 rows already deleted in the delete_or_not function. Hence, for =1 to M is not correct
No, it will not crash if you make the changed proposed by Amit.
I added it again. Thanks. It worked now. Thanks for your explaination

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.