Santosh Vernekar wrote: > Your program works fine because even if you have freed the memory, > it has not been reused/re-allocated yet, so the pointer pointing to this > portion > still holds the same data and you can work on it. > > However this might fail when you free the memory and try some new > allocations, > then now try to access the memory (probably you wont get the same data, > as it might have been reused). > Now the pointer starts misbehaving. So it is a common practice to > NULLify the > pointer which you have freed, so that the program doesn't make any more > access > to the freed portion. > > > Santosh Vernekar > Engineer > > - "They say that if you play a microsoft disc backwards you hear satanic > messages, > thats nothing: if you play it forwards it installs windows"
No. Wrong. It only works because the internal allocator has not given the address to another portion of the program _AND_ has not released the page back to the OS. The allocator is hanging onto the page for future allocations. Had it released the page and you attempted a memory access at the same address, a segmentation fault would have happened. You state, "Now the pointer starts misbehaving". Also false. The moment the memory is freed is the moment the pointer is to no longer be used (or in your words, "starts misbehaving"). This discussion is completely moot - undefined behavior is just that - undefined. Don't write code like that and you'll be fine. -- Thomas Hruska CubicleSoft President Ph: 517-803-4197 *NEW* MyTaskFocus 1.1 Get on task. Stay on task. http://www.CubicleSoft.com/MyTaskFocus/
