Context: I'm trying to wrap my head around pointers, we just saw them a couple of weeks ago in school and while practicing today I ran into a silly? issue, it can be super straightforward to you but I have little to none programming experience.
I've seen quite a few questions over in SO about deleting pointers but they all seem to be related to deleting a class and not a 'simple' pointer (or whatever the proper term might be), here's the code I'm trying to run:
#include <iostream>; using namespace std; int main() { int myVar, *myPointer; myVar = 8; myPointer = &myVar; cout << "delete-ing pointers " << endl; cout << "Memory address: " << myPointer << endl; // Seems I can't *just* delete it, as it triggers an error delete myPointer; cout << "myPointer: " << myPointer << endl; // Error: a.out(14399) malloc: *** error for object 0x7fff61e537f4: // pointer being freed was not allocated // *** set a breakpoint in malloc_error_break to debug // Abort trap: 6 // Using the new keyword befor deleting it works, but // does it really frees up the space? myPointer = new int; delete myPointer; cout << "myPointer: " << myPointer << endl; // myPointer continues to store a memory address. // Using NULL before deleting it, seems to work. myPointer = NULL; delete myPointer; cout << "myPointer: " << myPointer << endl; // myPointer returns 0. }
So my questions are:
Sorry for the long question, wanted to make this as clear as possible, also to reiterate, I have little programming experience, so if someone could answer this using layman's terms, it would be greatly appreciated!
The pointer itself does have an address and the value. The address of the pointer does not change after you perform delete on it. The space allocated to the pointer variable itself remains in place until your program releases it (which it might never do, e.g. when the pointer is in the static storage area).
Deleting a pointer (or deleting what it points to, alternatively) means delete p; delete[] p; // for arrays. p was allocated prior to that statement like p = new type; It may also refer to using other ways of dynamic memory management, like free free(p); which was previously allocated using malloc or calloc.
The delete operator is used to delete the pointer, which is either allocated using new operator or a NULL pointer, whereas the free() function is used to delete the pointer that is either allocated using malloc(), calloc() or realloc() function or NULL pointer.
The only thing that ever gets deleted is the object *test , which is identical to the object *test2 (since the pointers are the same), and so you must only delete it once.
1 & 2
myVar = 8; //not dynamically allocated. Can't call delete on it. myPointer = new int; //dynamically allocated, can call delete on it.
The first variable was allocated on the stack. You can call delete only on memory you allocated dynamically (on the heap) using the new
operator.
3.
myPointer = NULL; delete myPointer;
The above did nothing at all. You didn't free anything, as the pointer pointed at NULL.
The following shouldn't be done:
myPointer = new int; myPointer = NULL; //leaked memory, no pointer to above int delete myPointer; //no point at all
You pointed it at NULL, leaving behind leaked memory (the new int you allocated). You should free the memory you were pointing at. There is no way to access that allocated new int
anymore, hence memory leak.
The correct way:
myPointer = new int; delete myPointer; //freed memory myPointer = NULL; //pointed dangling ptr to NULL
The better way:
If you're using C++, do not use raw pointers. Use smart pointers instead which can handle these things for you with little overhead. C++11 comes with several.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With