![]() |
|
|
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 02:12 PM |
| delete in c++ | nandlal | High Level Programming | 4 | 02-02-2008 12:48 PM |
| Can't delete anything in /usr | ms1951 | UNIX for Dummies Questions & Answers | 4 | 10-22-2004 12:49 PM |
| delete | lesstjm | UNIX for Dummies Questions & Answers | 3 | 11-08-2001 03:24 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
C++ = new and delete
This is one basic question regarding C++ new and delete operator.
# include <iostream> int main() { int *ptr = new int(10); int i=0; for (i=0 ; i<10 ;i++) ptr[i] = 10; delete [] ptr; printf("%d",ptr[5]); // It prints the value 10 } In the above program memory for the *ptr is created using new operator and the same memory is deleted using the delete. But after deleting the ptr also I can able to access the memory location using the ptr . So any one please explain me what new() and delete() actually does. |
|
||||
|
Quote:
The reason you're able to use that memory after deleting it, is because it still exists. To oversimplify a bit, the new operator doesn't create new memory, the memory already exists; the new operator is there to arbitrate who gets what piece of memory. So when you free the memory, it still exists, but now the new operator "knows" it's allowed to give it to someone else. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|