Thread: Vectors and memory

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    Vectors and memory

    I just wanted to know "do vectors store there values in memory consecutively like a normal array from start to end? Or can they be broken up.
    By this i just mean in one instance of time, Im not talking about reallocation after the string has been modifyed or anything like that

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A normal implementation of a vector will store its elements in a consecutive block of memory, just like a dynamically allocated array. I'm not sure if the standard guarantees this or not, but if you use gcc at least that will be the representation.

    The idea of a vector is that it support random access fairly efficiently -- if that's what you need, you can use a vector without really worrying about how it is implemented . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Matty_Alan View Post
    I just wanted to know "do vectors store there values in memory consecutively like a normal array from start to end? Or can they be broken up.
    By this i just mean in one instance of time, Im not talking about reallocation after the string has been modifyed or anything like that
    The standard does not guarantee it, but this is considered an oversight in the standard. All realistic implementations of std::vector store their contents in a contiguous array of memory. If it were otherwise, the vector would not be able to satisfy the complexity requirements dictated by the standard.

    So you can assume that the contents of the vector occupy a contiguous region of memory from &vec.front() to &vec.back().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    Thanks guys, Im mainly just playing with vectors iv'e never really used them before but the general idea that iv'e picked up is that there just an array thats with a little more flexiablity.

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I have seen some earlier versions that (depending on certain system constraints and compile switches) would say start with a contiguous block of 64 elements, then if more were needed it could either allocate a larger block and move the old contents to the new location OR it would just allocate a new smaller or equal sized block and just maintain a link between the two depending on the indices requested, saving (temporarily) the time needed to move all of the old data. I am not saying this was right or even still in play but I did see it...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by brewbuck View Post
    The standard does not guarantee it, but this is considered an oversight in the standard. All realistic implementations of std::vector store their contents in a contiguous array of memory. If it were otherwise, the vector would not be able to satisfy the complexity requirements dictated by the standard.

    So you can assume that the contents of the vector occupy a contiguous region of memory from &vec.front() to &vec.back().
    According to my searches, that was true of the early standard of C++, but this oversight was corrected in C++03
    Is std::vector contiguous?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and vectors
    By larne in forum C Programming
    Replies: 6
    Last Post: 09-12-2008, 10:27 AM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  4. Working with dynamic memory in C++
    By IndioDoido in forum C++ Programming
    Replies: 8
    Last Post: 10-31-2007, 03:58 PM
  5. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 03:06 PM