Thread: question about vector

  1. #16
    Registered User
    Join Date
    Apr 2005
    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.

  2. #17
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Phildelphia, PA
    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.

    "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.

  3. #18
    Registered User
    Join Date
    Apr 2005
    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?

  4. #19
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Yes, that's exactly what it means. Also, the memory is in an undefined state at computer startup to begin with.
    All the buzzt!

    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying Array to Vector Question
    By bengreenwood in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2009, 03:00 PM
  2. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. Question about view vector, position, and matrix speed
    By Silvercord in forum Game Programming
    Replies: 1
    Last Post: 02-03-2003, 12:37 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM