View Single Post
Old Aug 30th, 2007, 5:31 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You cannot free memory which you have not gotten from the heap. Local variables such as tempBuffer are autovariables. These are allocated (in most implementations, on the stack) when the function is entered. They are deallocated when the function returns.

When you use free, you are telling the system to free heap memory located at the pointer that you received when the memory was allocated. Since the auto variable was not allocated from the heap, the heap has no record regarding it, and has little choice but to run off into the weeds and puke on its shoes.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote