Thread: C++ Vectors versus normal arrays

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    85

    C++ Vectors versus normal arrays

    I've been going more seriously into standard objects of C++. The vector seems a good one - but when should I use it, and when should I use the normal array (the one with square brackets) ?

    Also, how can I declare a multidimensional array and vector? Up to now, I've been using only one dimension for arrays

  2. #2
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    always use vectors!
    i don't know of a really good reason to not use them in c++... they haven't held me back yet.

    2d vector:
    vector< vector<int > > v2d;

    2d array:
    int test[10][12];
    Last edited by simpleid; 08-09-2007 at 01:11 PM.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    You can use static arrays when you know the number of items to be stored, at compile time (not at run-time). You should use dynamic arrays when this condition is not met.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    well, with vectors you can specify a static (starting) value to immediately allocate space for what ever it is your storing... so static amount of data doesn't really mean you need c-style arrays.

    i do wonder if there's any performance loss/gain though, maybe overhead? it would only be memory though i'm guessing, most pc's these days have plenty of that though.


    edit: ah hah! from the creator of c++ [arrays are gross]
    http://www.research.att.com/~bs/bs_faq2.html#arrays

    uses vectors for his example of a simple program, because vectors rock? :-)
    http://www.research.att.com/~bs/bs_f...simple-program
    Last edited by simpleid; 08-09-2007 at 02:56 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There can be performance benefits to using C style arrays when the size is a compile-time constant. There's nothing wrong with using vector in all cases, since those gains are often not noticable.

    Also, there is a new container that is not quite standard that can be used for static arrays (i.e. arrays with compile time constant size). That container is called array and takes the size of the array as one of its template arguments. I know it is available from boost and I believe it is part of TR1 as well. If you want a C++ alternative to static arrays then use that. For dynamic arrays use vectors.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    With compile-time sized arrays, you may gain a little bit of performance over run-time sized ones, because the compiler can use tricks to quickly calculate a fixed multiplication. If the compiler doesn't know the size when it compiles, it obviously can't use such tricks, and must resort to proper multiplication. This is particularly "bad" if the size is a power-of-two (2, 4, 8, 16, 32 ...).

    Whether this is at all noticable depends very much on how much data is being used, and how much of the time is spent walking the array data. In most cases, it's very likely that there will be no noticable difference.

    As to usage of memory - if you don't actually "touch" the memory, it's most likely not going to actually take up any physical space in your machine.

    Say you add a global variable like this to a small application:
    Code:
    int a[10000000];  // 40MB roughly.
    Then run the application and see how much memory it uses. I think you'll find that it uses roughly the same as without such an array. Add a loop to fill the array, and memory usage goes up to around 40MB.

    --
    Mats

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    "vector is the type of sequence that should be used by default." ([C++03] §23.1.1)
    If you doubt this advice, ask yourself if you really have a compelling reason not to use the only standard container that guarantees all of the following properties. vector alone is:

    • Guaranteed to have the lowest space overhead of any container (zero bytes per object).
    • Guaranteed to have the fastest access speed to contained elements of any container.
    • Guaranteed to have inherent locality of reference, meaning that objects near each other in the container are guaranteed to be near each other in memory, which is not guaranteed by any other standard container.
    • Guaranteed to be layout-compatible with C, unlike any other standard container. (See Items 77 and Chapter 78)
    • Guaranteed to have the most flexible iterators (random access iterators) of any container.
    • Almost certain to have the fastest iterators (pointers, or classes with comparable performance that often compile away to the same speed as pointers when not in debug mode), faster than those of all other containers.
    See: http://www.ubookcase.com/book/Addiso...6lev1sec2.html

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Then run the application and see how much memory it uses. I think you'll find that it uses roughly the same as without such an array. Add a loop to fill the array, and memory usage goes up to around 40MB.
    Its strange. Actually with static array it should never use more than its initial usage. Because as I know that memory is allocated on the stack. Your program should not run. Maybe its a kind of compiler optimization. Because in my compiler in Debug mode it generates an error at run-time "Stack Overflow" but in optimized mode program runs. Or maybe in optimized mode it doesn't check for overflow at run-time.
    Last edited by siavoshkc; 08-10-2007 at 10:48 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    ah hah! from the creator of c++ [arrays are gross]
    Both the problems mentioned should be fixed by std::tr1::array, which I believe was taken from boost's array, which in turn was inspired by Stroustrup's own c_array.

    uses vectors for his example of a simple program, because vectors rock? :-)
    No, because vectors are appropriate for that situation as the number of numbers to be read in is not known at compile time.

    Actually with static array it should never use more than its initial usage. Because as I know that memory is allocated on the stack. Your program should not run. Maybe its a kind of compiler optimization. Because in my compiler in Debug mode it generates an error at run-time "Stack Overflow" but in optimized mode program runs.
    I agree, it would be a compiler optimisation. The "Stack Overflow" error is probably because the statically allocated array is too large to fit on the stack, at least without this optimisation. If it could fit on the stack, there is not reason why the program would not run, even without the optimisation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    The "Stack Overflow" error is probably because the statically allocated array is too large to fit on the stack
    Of course. This can be resolved by changing the linker option for stack size.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    oops (delete) :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays vs Vectors vs Lists for particle rendering?
    By indigo0086 in forum Game Programming
    Replies: 4
    Last Post: 07-01-2007, 09:28 PM
  2. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  3. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM