top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C++: Difference between vector and an array?

+3 votes
904 views
C++: Difference between vector and an array?
posted Dec 5, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

array is just a class version of the classic C array. That means its size is fixed at compile time and it will be allocated as a single chunk (e.g. taking space on the stack). The advantage it has is slightly better performance because there is no indirection between the object and the arrayed data.

vector is a small class containing pointers into the heap. (So when you allocate a std::vector, it always calls new.) They are slightly slower to access because those pointers have to be chased to get to the arrayed data... But in exchange for that, they can be re-sized and they only take a trivial amount of stack space no matter how large they are.

answer Dec 5, 2013 by Salil Agrawal
0 votes

The size of std::array is fixed at compilation time, and no extra memory is used besides the storage of the array elements.

The size of std::vector can change dynamically as the program executes, and you pay for this flexibility with a size overhead of 3 pointers versus std::array. Access time is identical.

So for example if you want to store 3 space coordinates (x, y, z), and you want indexed access, std::array is a better choice. If you want to store a bunch of these triplets, and you don't know how many you'll have to store, put them in a std::vector.

So for example you could write:

typedef std::array<float, 3> Position;
typedef std::vector<Position> Positions;
answer Nov 20, 2014 by Manikandan J
Similar Questions
+1 vote

I wonder how one could get the compiler to generate the "movdqu" instruction, since the vector extensions always seem to assume that everything will be aligned to 16 byte.
I tried using a packed struct and this dint help much. Of course one can always resort to inline assembly but this should not be necessary

Compile with:

gcc -O2 -S -msse2 testvecs.c

Using built-in specs.

COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i486-linux-gnu/4.7/lto-wrapper
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5' 
--with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs 
--enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr 
--program-suffix=-4.7 --enable-shared --enable-linker-build-id 
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext 
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu 
--enable-libstdcxx-debug --enable-libstdcxx-time=yes 
--enable-gnu-unique-object --enable-plugin --enable-objc-gc 
--enable-targets=all --with-arch-32=i586 --with-tune=generic 
--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu 
--target=i486-linux-gnu
Thread model: posix
gcc version 4.7.2 (Debian 4.7.2-5)
...