Is it safe to delete an array pointer in C++ after reassignment?

c++ delete array element
delete function in c
delete array of pointers c++
how to delete an array in c++
malloc
delete pointer c++
delete dynamic array c++
delete heap array c++

Suppose I'd like to swap two array pointers in C++ which I have allocated with new [] and different length. Is it safe to delete [] them after swapping? What if they have the same length?

int *a;
int *b;
int *tmp;

a = new int[5];
b = new int[10];
tmp = a;
a = b;
b = tmp;

delete [] a;
delete [] b;

Yeah, you can. The reason being is that the information needed to accurately free up that memory is managed by the free store (the heap in most cases). So it knows how to delete based on address rather than variable. Often times this information is held in *(ptr - x) where x is the size of the tracking data.

Memory Management, C++ FAQ, Make sure to do the delete when an exception is pointers might be; // null, but that's okay since it's legal to delete null. Finally, note that pointers and arrays are evil. A lot of people have already commented saying that no, it's not safe to delete a void pointer. I agree with that, but I also wanted to add that if you're working with void pointers in order to allocate contiguous arrays or something similar, that you can do this with new so that you'll be able to use delete safely (with, ahem, a little of extra work). This is done by allocating a void pointer to the memory region (called an 'arena') and then supplying the pointer to the arena to new.

Is it safe to delete [] them after swapping?

Yes.

What if they have the same length?

The length is irrelevant.

delete() in C++, delete() in C++. Delete is an operator that is used to destroy array and non-array(​pointer) objects which are created by new expression. Delete can be used by  Delete is an operator that is used to destroy array and non-array(pointer) objects which are created by new expression. Delete can be used by either using Delete operator or Delete [ ] operator New operator is used for dynamic memory allocation which puts variables on heap memory.

Yes, you can surely delete the pointer once your swapping is done. As you no longer need to point your array so in that case, it is safe to delete after swapping.

int *a;
int *b;
int *tmp;

a = new int[5];
b = new int[10];
tmp = a;
a = b;
b = tmp;

delete [] a;
delete [] b;

And this is the correct way of doing it. Great Job

operator delete[] - C++ Reference, Non-member array deallocation functions shall not be declared in a namespace scope other than the global namespace. Parameters. ptr: A pointer to the memory​  Pointer and Arrays in C. When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of the array. Base address i.e address of the first element of the array is also allocated by the compiler.

Can I delete pointers allocated with malloc()?, C++ FAQ, Beware! Sometimes people say, "But I'm just working with an array of char." Nonetheless do not mix malloc() and delete on the same pointer  It is safe, if you do not access the this pointer, implicitly or explicitly after calling delete on it. This idiom is used in some smart pointer classes - It seems like a very bad idea though, because it assumes that the object exists on the heap and was allocated using new. If you use "delete this", the class should be impossible to construct on the stack or static heap, which means a private constructor and a factory function which is a friend.

Can I drop the [] when deleteing an array of some built-in type (char , C++ FAQ, Celebrating Twenty-One Years of the C++ FAQ! 16.2, Is it safe to delete the same pointer twice? [16.13] Can I drop the [] when deleteing an array of some built-in type (char, int, etc)?. No! Sometimes programmers think that the [] in the delete[] p only exists so the compiler will call the appropriate destructors for  Just like for SafeArrayDestroy, the only parameter is a pointer to a safe array descriptor. On success, SafeArrayLock increments a lock count associated to the input safe array and places a pointer to the array data in the pvData field of the safe array descriptor. As its “pv” prefix suggests, the pvData field is a “void*” pointer.

Top 10 Most Common C++ Mistakes That Developers Make, Common Mistake #1: Using “new” and ”delete” Pairs Incorrectly To avoid such C++ issues, a safer way of reusing of a class/template from the standard There is no guarantee for every compiler that a pointer to an array will point to the first  In most contexts, array names decay to pointers. In simple words, array names are converted to pointers. That's the reason why we can use pointers to access elements of arrays. However, we should remember that pointers and arrays are not the same. There are a few cases where array names don't decay to pointers.

Comments
  • Yes it's safe. As long as delete[] gets the same pointer values as were returned by new[] then it's OK.
  • @john So how does the compiler keep track of how much memory can be freed for each pointer?
  • Don't use raw pointers, C-style arrays, new or delete. std::vector is your friend.
  • @elzell The compiler doesn't keep track. The same way that free knows how much memory to free with malloc in C, some extra book-keeping information is stored with the allocated memory. It's safe in that the memory will be properly freed, but I am obligated to say that you almost never should use new[] and delete[] and should just use a std::vector instead.
  • It isn't the variable (a, b) that matters, it's what they point to (arrays of 5 and 10 integers). As long as you delete all of your dynamically allocated memory once and once only you should be OK. std::vector (or std::unique_ptr) can make this a lot simpler.
  • And this is the correct way of doing it. Great Job std::unique_ptr<T[]> or std::vector<T> would be the correct way. new and delete should not use used unless you absolutely have to (your writing you own library implementation)
  • @NathanOliver definitely that is a more appropriate way of doing it. Though for small code like swapping delete will do the job.
  • @NathanOliver Both are correct. Yours is preferred.