I won't try to access elements that i removed but i wanted to know why they exists after i erase them. also i don't want the memory to keep data which i don't need.
I won't try to access elements that i removed but i wanted to know why they exists after i erase them. also i don't want the memory to keep data which i don't need.
If you're that concerned about memory, then you'd be better off using an array and resizing it yourself. The STL with its heaping amounts of usefullness and ease comes with overhead.
vectors are destroyed as soon as they go out of scope. If you want to make sure a vector is destroyed at some point, put it in a block of code so that it'll be destroyed as soon as you're done with it.
When you clear() or delete[] memory, you're not erasing something like you would penciled remarks on paper. Your computer doesn't get out it's eraser and rub out what's in that memory. What's really going on is that that memory is being freed. That means that if anybody else needs it, it's there. But until it's used by anyone else, it just sits there with whatever used to be in there.
As someone else said, referencing freed memory is undefined. What you did was free up some memory and then take a look at what was there. Chances are good that that memory wasn't yet used by another process, so it's still sitting there. That doesn't mean you should be using it, however, because if another process needs some memory and overwrites that area of memory, then when you try to access it next, you'll get garbage.
Last edited by joshdick; 04-28-2005 at 08:01 AM.
FAQ
"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.
"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.
so when i'm trying to access some data in the memory and i get weird letters it means that the memory was freed and was used by another program?
Yes, that's exactly what it means. Also, the memory is in an undefined state at computer startup to begin with.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law