Removing dynamically allocated variables specifying a pointer to 0

I can not understand the end of this code ( array = 0; ):

 #include <iostream> int main() { std::cout << "Enter a positive integer: "; int length; std::cin >> length; int *array = new int[length]; std::cout << "I just allocated an array of integers of length " << length << '\n'; array[0] = 5; // set element 0 to value 5 delete[] array; // use array delete to deallocate array array = 0; // use nullptr instead of 0 in C++11 return 0; } 

In the end, the dynamically allocated array is deleted (returned to the OS), and then the value 0 is assigned.

Why is this done? After the array has been returned to the OS, there is no need to assign it a value of 0, right?

Code from: http://www.learncpp.com/cpp-tutorial/6-9a-dynamically-allocating-arrays/

+7
c ++ memory-management arrays pointers dynamic-allocation
source share
4 answers

After the array has been returned to the OS, there is no need to assign it a value of 0, right?

You are right, it is not needed, because memory is freed (freed) by the delete operator. But think about the case when you can use the pointer elsewhere in your code (functions, loops, etc.) after using delete[] on it.

The array variable still contains the address of the old selection after the delete[] operator was called (dangling pointer) . If you accessed this address, you would get undefined bahaviour (UB) , because the memory is no longer yours, in most cases your program will work.

To avoid this, follow a null pointer , for example:

 if (array != nullptr) { /* access array */ ... } 

which checks for a pointer to address 0, which represents an invalid address.

To do this check, you can set the pointer to nullptr or NULL if C ++ 11 is not available. The nullptr introduces type safety, since it acts as a pointer type and should be preferred over C-like NULL . In pre C ++ 11, NULL is defined as the integer 0, since C ++ 11 is the nullptr alias.
To define your own nullptr to use it for the pre C ++ 11 compiler, look here: How to define our own nullptr in C ++ 98?


An interesting fact about delete or delete[] is that it is safe to use it on nullptr . This is written in paragraph 2 on cppreference.com or in this SO answer .

delete operator, delete operator []

2) [...] The behavior of the standard library implementation of this function is undefined if ptr not a null pointer or is a pointer previously obtained from the standard library implementation operator new[](size_t) or operator new[](size_t, std::nothrow_t)

+15
source share

We set pointers to NULL (0) to avoid dangling pointers (the pointer still points to the same memory that no longer belongs to you). In the case of local variables, this is not so useful if the function does not continue after deletion (therefore, its obvious pointer will not be reused). In case global / participants can use their good practice to avoid mistakes.

Accessing an already deleted pointer can overwrite / read random memory (this can be more dangerous than a crash) and causes undefined behavior , while accessing the NULL pointer will immediately fail.

Since C ++ 11 , you should use nullptr , because its type is a pointer type, and NULL larger than int , and improves type safety + eliminates ambiguous situations.

If you delete the pointer nullptr , it is safe to use delete on nullptr , and nothing happens, but if you delete the already deleted non-zero pointer, it will cause undefined and, most likely, the program will crash.

In C ++, you should avoid using pure pointers, as they are STL containers (which release their resources themselves ( RAII )) for that use or smart pointers .

 std::vector<int> array{1,2,3,4,5}; 
+8
source share

This is done so that the pointer is set to NULL (be it in C ++, preverer nullptr , since NULL and 0 can be different things).

This tactic eliminates the possibility of a dangling pointer because the array can be deleted, but that does not mean that it is set to NULL .

If we do not, we risk checking whether the pointer is NULL or not (the last in our code), we will see that it is not NULL , we mistakenly assume that the pointer is in order to access and call Undefined Behavior.

+3
source share

You assign a value, usually called an "invalid address", i.e. NULL , 0 or the pointer type is nullptr , because otherwise you cannot find out if the pointer points to an invalid address. In other words, when you delete[] your pointer array β€œdoesn't know” that it points to a memory address that is no longer usable.

+1
source share

All Articles