vector resizing

This is a discussion on vector resizing within the C++ Programming forums, part of the General Programming Boards category; I thought vectors were supposed to resize themselves. But this crashes on my compiler: Code: #include <vector> int main(void) { ...

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046

    vector resizing

    I thought vectors were supposed to resize themselves. But this crashes on my compiler:
    Code:
    #include <vector>
    
    int main(void) {
        std::vector<int> v;
    
        for(int x = 0; x < 10; x ++) {
            v[x] = x;
        }
    
        return 0;
    }
    But this doesn't:
    Code:
    #include <vector>
    
    int main(void) {
        std::vector<int> v(10);
    
        for(int x = 0; x < 10; x ++) {
            v[x] = x;
        }
    
        return 0;
    }
    What's happening here?
    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.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You're using random access to access an index that doesn't exist before you try to add an element to the vector.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    you need to use the push_back( ) member function to put stuff into your vector.. then it will resize itself when necessary...

    you can then use the [ ] subscript operator with your vector class object.. just like an array.. but only after the vector has been properly populated.
    Last edited by The Brain; 01-06-2006 at 03:54 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    A vector has a given size at any time. Accessing an element outside this size will produce undefined behaviour.

    A vector will not resize itself to fit your indices. It will automatically manage memory if you request it to resize itself. In other words, if you use insert() or push_back(), you can add elements as long as it can find a large enough block of memory, and you don't need to handle the memory management.
    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

  5. #5
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    As far as I understand though, if you push an element onto a Vector so that is must resize itself, it expand to twice it's current size (double memory usage). For a small amount of items it's okay, but pushing that million and oneth element onto a Vector with one million spots will result in a Vector two million spots large.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    I thought that the preferred way to do things was to start with an 'empty' vector and then resize as necessary, as vectors grow efficiently??

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Epo
    As far as I understand though, if you push an element onto a Vector so that is must resize itself, it expand to twice it's current size (double memory usage). For a small amount of items it's okay, but pushing that million and oneth element onto a Vector with one million spots will result in a Vector two million spots large.
    If you already have a million items, chances are that you're going to push another 1000 instead of just one. And another 1000 soon after that.

    Quote Originally Posted by kermit
    I thought that the preferred way to do things was to start with an 'empty' vector and then resize as necessary, as vectors grow efficiently??
    It's always more efficient to get the size right the first time. So if you know it beforehand, at least use reserve() to make the vector allocate enough space.
    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

  8. #8
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    Quote Originally Posted by kermit
    I thought that the preferred way to do things was to start with an 'empty' vector and then resize as necessary, as vectors grow efficiently??
    I guess it depends on how you use them.. if you have a vector of ptrs or ints or simple objects its not bad..

    if you have a vector of objects it could get a little messy

    vector<foo> v;

    because..
    everytime it runs out of space it copies all objects into new space and destroys the old ones... so if the objects are large, it could get rather expensive even with a reasonable sized vector..

    though i imagine common practice is to use vector to store ptrs.. atleast i try to always do that..

    one other way to do it is to use vector::reserve(int) to reserve space.. this does not create x amounts of objects.. just gets enough contigious space for them..

    thats my understanding of vectors anyhow...

    <edit>oops didnt see your post CornedBee</edit>
    Last edited by xhi; 01-06-2006 at 06:08 PM.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I thought that the preferred way to do things was to start with an 'empty' vector and then resize as necessary, as vectors grow efficiently??
    If vectors resized every time you added an element, that would be inefficient. So, they double in size(or more depending on the implementation) when they are resized. If you need to add only one more element and the vector has to resize to allow you to do that, then the resizing is inefficient in that the vector allocates more memory than is required.
    Last edited by 7stud; 01-06-2006 at 07:17 PM.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, but my vector isn't resizing at all. I get a segmentation fault!
    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.

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    need code.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What do you mean? See the first post in this thread.
    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.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You're using random access to access an index that doesn't exist before you try to add an element to the vector.
    So you can't do this then? I thought you could.
    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.

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    No you can't.

    edit::

    No you shouldn't..

  15. #15
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    push_back(), insert() and the like will resize for you.. by index is just for accessing..

    if you use a map, it will work like what you are doing though.. resizing by assigning to an index..

    Code:
    #include <map>
    #include <string>
    
    int main(void) {
        std::map<int, string> m;
    
        for(int x = 0; x < 10; x ++) {
            m[x] = someString;    
        }
    
        return 0;
    }
    hth

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21