![]() |
|
#1
|
|||
|
|||
Dynamic memory allocation - Dangling pointers revisitedCould you help me detect the problem. As i suspect there must be a dangling pointer to this code.But it behaves fine.
When it comes to the destructor code also, after delete [] ptr, it is printing the values correctly as it is unexpected and that the heap memory should have been reclaimed. CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
|
Quote:
When you delete (or delete []) something, it tells the system that you aren't going to use that memory any more, so it is returned to the heap. If you decide to use that memory after you have deleted it, that is called "undefined" behavour. That is, the contents of that memory may be the same as before you deleted it or not. Furthermore, there is no way that the system can go back and change the values of any variables that you have used to point to that memory, so the pointer values still point to the memory that used to be yours, even though it isn't yours anymore. It is the program's responsibility to keep track of its pointers. Many people make a practice always to put in code to set the pointer to NULL after a delete so that inadvertant (buggy) subsequent attempts to dereference that pointer will result in an access violation (segment fault). Regards, Dave |
|
#3
|
||||
|
||||
|
CPP / C++ / C Code:
in short, i think thats what dave meant __________________
People should read the rules and regulation before posting! The Best is yet to be... |
|
#4
|
|||
|
|||
|
Quote:
In fact, in C++, "delete" doesn't care if the pointer is null or not. The Standard says that if you give "delete" a null pointer it will ignore it, so this test is useless (but it's also harmless). My point was, and is, that the program must do the right thing: after delete is used on a pointer (that was given memory by "new"), any attempt to dereference the pointer results in undefined behavior. Regards, Dave |
|
#5
|
|||
|
|||
|
Quote:
Thanks for the valuable information and that i got a good picture on dynamic memory deallocation. My doubt my sound silly or trivial but i would like to know as to what will happen without delete operator and that i am trying to make the pointer to NULL in destructor. CPP / C++ / C Code:
I definitely know that this is the invitation for disaster as it will lead to a memory leak. This is what most of the books claim and that they touch upon this topic simply saying it frees the memory allocated by new. But then what is the internal function of delete operator? I am having this doubt because even after deleting the ptr, the link to the heap through the pointer (that has been deleted) still exists and not withdrawn.We were still able to access the values and infact even modify values and print the same.Anyway it the responsibility of the Programmer to take care. Actually my understanding(may be even assumption) on "delete" operator is that it withdraws the link from the heap by making the ptr value NULL immediately after delete. So my expectation was that at the worst case, i was expecting something like dereferencing the ptr would lead to catastrophic results. But this did not happen as i was able to access the values even after deleting the ptr. Considering all these, what exactly is the function of delete operator? I would hear for a brief explanation in terms of compiler treating the delete operator. Thanks and Regards, Karthikeyan.S INDIA |
|
#6
|
||||
|
||||
|
new will grab a bag of bytes from the heap and add pointers and such to tell the system 'this memory block is in use elsewhere. Keepa-U hands off!' The memory is not cleared so if you start looking at the bytes immediately after allocation the data will contain whatever was there when some other program had the memory. Similar to renting an apartment that was just vacated by someone who didn't clean it. All the dirt and mess is now yours.
delete simply reverses the allocation. You, as the current occupant simple leaves and a "For Rent" sign is put out so the memory can be allocated to someone else. But since you still have a key (the address/pointer) you can visit and see the stuff you left. But when another process gets allocated that memory, they get to have all your stuff and change the locks. __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#7
|
|||
|
|||
|
Quote:
Great explanation, Walt. When I said "this test is useless", I meant that testing for non-NULL before calling delete is useless. I should have made it clear that this is what I meant: You should always call "delete" for every thing you got with "new". You pass the pointer value that "new" gave you. If you happen to pass a NULL-valued pointer, "delete" is required to ignore it. If you pass any other value (other than something that "new" gave you), the behavior is undefined. Regards, Dave |
|
#8
|
||||
|
||||
|
Hmm, so delete is to actually ignore ? lol dint know that .
Quote:
Does this mean that u ask your ptr to pt to something else like halfway thru your program ? like: CPP / C++ / C Code:
__________________
People should read the rules and regulation before posting! The Best is yet to be... |
|
#9
|
|||
|
|||
|
Quote:
Now, when I said the "test is useless" I meant that you don't have to test to see if ptr is NULL before you execute "delete ptr". I didn't mean that you don't have to "delete ptr". Walt explained it very well. Now as to your example: ptr is a pointer to char, so ptr[0] is a char --- it is semantically the same as (*ptr). ch1 and ch2 are chars, so &ch1 and &ch2 are pointers to char. Your program tries to set a char equal to a pointer to a char. Not allowed in C++. Once you are given memory (by using "new") you can put anything you want to into that memory. Once you have released the memory back to the operating system (by using "delete"), you must not access that memory again. (And if you don't release the memory back to the operating system, that is called a "memory leak" --- a Bad Thing.) In the following example, I have commented out the two illegal statements. If you uncomment them, the behavior is undefined. One compiler might give one result and another compiler might give another. A program compiled by one compiler could give one result now and a different result if the exact same program is executed tomorrow. (That's what "undefined behavior" means.) CPP / C++ / C Code:
Here is something else that is illegal; maybe that's what you had in mind: CPP / C++ / C Code:
Regards, Dave |
|
#10
|
||||
|
||||
|
Quote:
Quote:
CPP / C++ / C Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 18:36 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 19:35 |
| Dynamic Memory Allocation - Offset Pointers | wu_weidong | C Programming Language | 6 | 24-Feb-2005 10:48 |
| 3D array dynamic memory allocation | cjwatchdog | C Programming Language | 3 | 20-Feb-2004 16:27 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The