0

I'm Trying To avoid memory lack and I need to understand if I need to set nullpointer to my Multidimensional dynamic array after deleting an array. Here is my code.

int*** arr = new int** [lists];

for (int i = 0; i < lists; i++)
{
    arr[i] = new int* [row];
    for (int r = 0; r < row; r++)
    {
        arr[i][r] = new int[col];
    }
}
for (int i = 0; i < lists; i++)
{
   
    for (int r = 0; r < row; r++)
    {
        for (int z = 0; z < col; z++)
        {
            arr[i][r][z] = rand() % 100;
        }


    }
}
for (int i = 0; i < lists; i++)
{

    for (int r = 0; r < row; r++)
    {
        delete[] arr[i][r];

        
    }
    
    delete[] arr[i];
}
delete[] arr;
arr = nullptr;
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Anything that was `new`ed, should be `delete`d. – Evg Aug 17 '20 at 10:56
  • 7
    *"I'm Trying To avoid memory leak"*, so use smart pointer and/or containers. avoid raw owning pointers. – Jarod42 Aug 17 '20 at 10:57
  • 2
    No, you don't need to `arr = nullptr;` to avoid memory leaks. The only reason to set it to `nullptr` is if you check it later (`if(arr) ...`) for some reason. – Ted Lyngmo Aug 17 '20 at 10:58
  • @Native Nova Memory leaks have nothing common with assigning or not assigning a pointer to nullptr after deleting the allocated memory. – Vlad from Moscow Aug 17 '20 at 10:58
  • 1
    It's not *necessary*, there will not be any memory leak if you don't null a pointer, but it leaves it in "dangling" state. If you never use it again, it's fine. If it's possible that you will use it again, it's best to set it to `nullptr`. – Yksisarvinen Aug 17 '20 at 10:58
  • Here `std::vector>>`, but some people prefer `std::vector` with method to compute index, for cache efficiency. – Jarod42 Aug 17 '20 at 11:00
  • Does this answer your question? [How do I declare a 2d array in C++ using new?](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – Jan Schultke Aug 17 '20 at 11:09
  • I suggest using a flat `std::vector` wrapped in some sort of multidimensional container, and calculate your own multidimensional position to the flat vector index. – Eljay Aug 17 '20 at 12:03
  • 2
    Don't be a [three star programmer](http://wiki.c2.com/?ThreeStarProgrammer). – n. m. could be an AI Aug 17 '20 at 12:17

4 Answers4

1

Is it necessary to null your pointers after deleting multidimensional dynamic array

It depends. Sometimes it is, other times it isn't.

When you need to inspect whether you still have a valid pointer, then you must set an invalid pointer to null because null pointer is the only invalid pointer that you can test for. Your example doesn't demonstrate that you would need to do so.

Quite often, delete is used in a place where the lifetime of the pointer is about to end, in which case it is entirely redundant to set it to null.

P.S. Don't use owning bare pointers like this. Use RAII containers such as std::vector or at least smart pointers.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Memory leak will not happen in your case , but for Good Practice you need to null your pointer after deleted that pointer , otherwise if you later dereference the same variable, your program will have undefined behavior

"unhandled exception error" may occur.

if(arr) 

also not help this kind of case. so use the null check of every pointer before access the pointer

tamil
  • 99
  • 8
  • 1
    "_otherwise later if you access the same variable ..._" - Should be: "otherwise if you later dereference the same variable, your program will have undefined behavior". – Ted Lyngmo Aug 17 '20 at 11:40
  • You will have undefined behaviour whether or not you set `arr=nullptr`. Setting `arr=nullptr` simply makes such illegal accesses easier to debug. – TonyK Aug 17 '20 at 11:41
  • 2
    " Good Practice you need to null your pointer after deleted that pointer" - I sit on the other side of the fence on that one; personally I think it leads to sloppy coding and the masking of bugs which are still bugs. – Bathsheba Aug 17 '20 at 12:46
0

This is really just a question of code hygeine. I think the main benefit of setting arr = nullptr after deletion is that you can then safely delete arr[] at any later time without worrying. It also makes it easier to catch bugs if you try to reference the deleted array.

So it's not compulsory, but it's something that I personally do as a matter of course.

TonyK
  • 16,761
  • 4
  • 37
  • 72
0

Use SmartPointers to avoid Memory leak issues.

No, you don't need to set the Pointer variable to "NULL", the program will run without issues.

Setting it to null can act as extra security to prevent usage of the variable by mistake.

-----------------------

Operator new reserves space so that any new memory allocation or for internal operations the compiler does not occupy those allocated memory locations.

Operator Delete just un-marks those reservations. The reference variable still has the address of the memory location and you can access it but now, it can be altered any time by the compiler.

Contents of the Pointer variable before and after delete Operation.

    int *a;
    cout<<"before memory allocation a= "<<a<<endl;
    a= new int[10];
    a[0]= 1; a[7]=2;
    cout<<"After memory allocation but Before Delete"<<endl;
    cout<<"a="<<a<<"\ta[0]="<<a[0]<<"\ta[7]="<<a[7]<<"\ta[22]="<<a[22]<<endl;
    cout<<"sizeof(a)="<<sizeof(a)<<"\tsizeof(a[7])="<<sizeof(a[7])<<"\tsizeof(a[22])="<<sizeof(a[22])<<endl;
    delete[] a;
    cout<<"After Delete"<<endl;
    cout<<"a="<<a<<"\ta[0]="<<a[0]<<"\ta[7]="<<a[7]<<"\ta[22]="<<a[22]<<endl;
    cout<<"sizeof(a)="<<sizeof(a)<<"\tsizeof(a[7])="<<sizeof(a[7])<<"\tsizeof(a[22])="<<sizeof(a[22])<<endl;
    return 0; 

Result:

before memory allocation a= 0x40ed39
Before Delete
a=0x9a1550      a[0]=1  a[7]=2  a[22]=1316552972
sizeof(a)=8     sizeof(a[7])=4  sizeof(a[22])=4

After Delete
a=0x9a1550      a[0]=10099280   a[7]=2  a[22]=1316552972
sizeof(a)=8     sizeof(a[7])=4  sizeof(a[22])=4
Darshan K N
  • 113
  • 8