"Delete" statement not working?

Hi there, Im recently writing a new code where a function returns a pointer, say *myp. Thing is inside this function, the pointer myp points towards an array of N elements, so in fact what the function is returning is the whole N dimensional array, with the pointer "myp" pointing to the first element. All good here, I use "cout" to see all the elements of the array and it seems to work. What worries me is that when Im done with the program, at the end, I want to erase the created space for the N dimensional array (because I used the "new" command inside the function), so I use:

delete[] vpoint.

where vpoint is a pointer defined in the MAIN function, not the function where "myp" is defined. After doing this, I use cout again for vpoint, say, vpoint[2], and the thing is that it still displays the value, so I guess it hasnt been deleted. Any guesses why is this happening? Thanks!!
delete frees up the memory so that it can be re-used. It doesn't necessarily change the contents of that memory. What you're seeing is just the data that was put into that memory previously.
vpoint[2] has been deleted. You're accessing an object that no longer exists using a pointer that is no longer valid, and you're observing undefined behavior, which, in your case, just happened to show leftover data still sitting in unused memory, and not crash or worse.

The hotel room key analogy (which is about accessing destroyed local variables) applies here as well: https://stackoverflow.com/a/6445794

Also, if you're using C++ (sounds like it since you mention cout), the correct type to use is std::vector, not any sort of pointer.
Last edited on
Thanks for the reply, does that mean that after using "delete" the adress of the pointer "vpoint" should not exist anymore? Or is the pointer still pointing to that location? Thanks
Thanks for the reply, does that mean that after using "delete" the adress of the pointer "vpoint" should not exist anymore? Or is the pointer still pointing to that location?


The pointer vpoint will still exist. It's a variable like any other, and will exist for as long as it's in scope, just like any other variable.

delete doesn't change the value of the pointer (which is an address). It simply tells the memory management system that the bit of memory at that address, which was previously reserved, is no longer reserved, and is available for re-use.

So, the vpoint variable will still exist, and it will still hold the same memory address as its value. But you can no longer use the memory at that address.

It's not unusual to see code such as:

1
2
delete myPtr;
myPtr = nullptr;


i.e. setting the value of the pointer to null, to indicate that it's no longer pointing to memory that can be used.
Last edited on
Just to be more clear, the fact that the function outside MAIN is creating a "new double" type array doesn't influence what Im erasing in the MAIN function, does it? In other words, am I losing any memory by doing this or looks correct? (I am a bit afraid of having some memory crashes when the program is about to run with large data sets)
OK, to pick apart some misunderstandings you seem to have, from your original post:

Im recently writing a new code where a function returns a pointer, say *myp. Thing is inside this function, the pointer myp points towards an array of N elements, so in fact what the function is returning is the whole N dimensional array


No. The function does not return "a whole array". The only thing it returns is an address. Inside your function, you have reserved a block of memory at that address for your array. That memory will remain reserved until you use delete to free it up. It doesn't matter where in your code you tell it to free it up.

So, in main, when you do:

delete[] vpoint;

you free up the memory that you reserved inside your other function.
Last edited on

delete doesn't change the value of the pointer (which is an address). It simply tells the memory management system that the bit of memory at that address, which was previously reserved, is no longer reserved, and is available for re-use


Okei got it now, thanks very much for your help guys!
Topic archived. No new replies allowed.