![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to delete content in a file (delete content only) | kittusri9 | Shell Programming and Scripting | 5 | 05-15-2008 01:12 PM |
| C++ = new and delete | arun.viswanath | High Level Programming | 3 | 09-13-2005 10:16 PM |
| Can't delete anything in /usr | ms1951 | UNIX for Dummies Questions & Answers | 4 | 10-22-2004 11:49 AM |
| delete | lesstjm | UNIX for Dummies Questions & Answers | 3 | 11-08-2001 03:24 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
My understanding is this: delete() frees the memory space allocated already. However, if you do not reset the pointer to NULL, it will continue to point to a freed memory block (which may subsequently be allocated for some other uses) and lead to segmentation fault or mysterious behaviour with unexpected value if you happen to access that pointer. To safeguard, reset the pointer to NULL as soon as the memory location is freed.
|
|
||||
|
I guess it is used for safeguarding from re-deleting some pointer that was already deleted in the past and for knowing which pointer references real object and which one is not. For example if you have some kind of a pool (array of pointers) of objects of some type that you manage manually (allocating new objects, deallocating old ones), you have to know, whether some particular pointer in the pool is actually valid. So when you deallocate an object from the pool, you set its pointer to NULL and then you know that this pointer may be used again in the future. In case of freeing the whole pool you have to delete only those pointers that are not NULL.
On my system (Ubuntu Linux 6.06) there is no difference in referencing NULLed and invalid pointer, I get segmentation fault in both cases. |
|
||||
|
Reuse of deleted pointer
As nutrino said After deleting the the class memory pointer becomes dangling pointer and there are many consequences of that and might affect to crash ur program also
I hope below article will help you Dangling pointers |
![]() |
| Bookmarks |
| Tags |
| linux, ubuntu |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|