Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete vs NULL vs free in c++

Tags:

c++

c

what is the difference between deleting a pointer, setting it to null, and freeing it.

delete ptr; 

vs.

ptr=NULL; 

vs.

free(ptr); 
like image 554
hero Avatar asked May 26 '10 06:05

hero


People also ask

What is the difference between delete and free in C?

delete and free() in C++ In C++, the delete operator should only be used either for the pointers pointing to the memory allocated using new operator or for a NULL pointer, and free() should only be used either for the pointers pointing to the memory allocated using malloc() or for a NULL pointer.

Is delete and free same?

free() is a C library function that can also be used in C++, while “delete” is a C++ keyword. free() frees memory but doesn't call Destructor of a class whereas “delete” frees the memory and also calls the Destructor of the class.

Which is faster free or delete?

The delete() operator is faster than the free() function.

What does free null do in C?

The C Standard specifies that free(NULL) has no effect: The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs.


2 Answers

Your question suggests that you come from a language that has garbage collection. C++ does not have garbage collection.

If you set a pointer to NULL, this does not cause the memory to return to the pool of available memory. If no other pointers point to this block of memory, you now simply have an "orphaned" block of memory that remains allocated but is now unreachable -- a leak. Leaks only cause a program to crash if they build up to a point where no memory is left to allocate.

There's also the converse situation, where you delete a block of memory using a pointer, and later try to access that memory as though it was still allocated. This is possible because calling delete on a pointer does not set the pointer to NULL -- it still points to the address of memory that previously was allocated. A pointer to memory that is no longer allocated is called a dangling pointer and accessing it will usually cause strange program behaviour and crashes, since its contents are probably not what you expect -- that piece of memory may have since been reallocated for some other purpose.

[EDIT] As stinky472 mentions, another difference between delete and free() is that only the former calls the object's destructor. (Remember that you must call delete on an object allocated with new, and free() for memory allocated with malloc() -- they can't be mixed.) In C++, it's always best to use static allocation if possible, but if not, then prefer new to malloc().

like image 61
j_random_hacker Avatar answered Sep 19 '22 20:09

j_random_hacker


delete will give allocated memory back to the C++ runtime library. You always need a matching new, which allocated the memory on the heap before. NULL is something completely different. A "placeholder" to signify that it points to no address. In C++, NULLis a MACRO defined as 0. So if don't like MACROS, using 0 directly is also possible. In C++0x nullptr is introduced and preferable.

Example:

int* a;        //declare pointer a = NULL;      //point 'a' to NULL to show that pointer is not yet initialized  a = new int;   //reserve memory on the heap for int  //... do more stuff with 'a' and the memory it points to  delete a;      //release memory a = NULL;      //(depending on your program), you now want to set the pointer back to                // 'NULL' to show, that a is not pointing to anything anymore 
like image 25
Lucas Avatar answered Sep 20 '22 20:09

Lucas