2

A vector is supposed to dynamically grow and has a contiguous memory location ,obtained by using operator new via allocator class.My question is ,how much memory does a default vector (when we do not specify a reserve size) allocate using new by default ,so it can grow and add elements later.

Straw
  • 63
  • 6
  • Why not use `sizeof()`? – HelloWorld123456789 Feb 26 '14 at 17:53
  • 2
    @RikayanBandyopadhyay Because `sizeof()` will simply tell you how large the `vector` type is; it doesn’t tell you how much space it has reserved for storage. – al45tair Feb 26 '14 at 17:54
  • 1
    You could check the output from vector.capacity() for an unused vector, and then multiply that with the size of an element. – dutt Feb 26 '14 at 17:55
  • sizeof always gives the size as 12 which looks like the size of vector object itself and the sum of sizes of its members..my question is regarding the memeory size vector reserves in advance for dynamic growth later by default.......nut not specified by reserve() method. – Straw Feb 26 '14 at 17:55
  • does vector.capacity work ???I tried adding one int element into a vector and an empty vector but it showed me vector.capacity as 0 and 4 respectively for an int vector. – Straw Feb 26 '14 at 17:56
  • To my that says that it doesn't allocate any initially and allocates 4 elements the first time you insert something. – dutt Feb 26 '14 at 17:58
  • by 4 I meant one integer of 4 bytes not 4 elements ..I dont think so its 4 elements – Straw Feb 26 '14 at 17:59

2 Answers2

3

It's implementation defined (i.e. may differ between multiple compilers).

The total amount of allocated memory can be queried using std::vector::capacity() function.


To read on, check out this post: size vs capacity of a vector?.

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
2

That is up to the implementation, and you have no way to know. Actually, even if you specify a size to reserve, the implementation is free to reserve however much it pleases (the size is just a hint, AFAIK).

al45tair
  • 4,405
  • 23
  • 30
  • AFAIK, this is wrong. reserve() has the observable post-condition that the next call to capacity() yields an equal or greater value. Just checked it here: http://www.gotw.ca/gotw/074.htm ("... the call to reserve() will guarantee that the vector's capacity() is at least 2"). – Christian Hackl Feb 26 '14 at 18:14
  • @ChristianHackl I don’t see why the `vector` implementation has to have allocated any memory at that point, whatever it returns from `capacity()`. For instance, it might choose to defer allocation until an attempt is made to access the memory. It could also choose to return a number larger than was passed to `reserve()` but smaller than it actually allocated (not sure why you’d want to, but I don’t see why it couldn’t). – al45tair Feb 26 '14 at 18:53
  • To be honest, I don't know if the wording of the standard would actually allow this or not. Would a technique like this (i.e. dealing with abstract values in capacity and reserve) make sense from a quality-of-implementation POV, anyway? – Christian Hackl Feb 26 '14 at 18:56
  • @ChristianHackl Perhaps, I don’t know. It would rather depend on how often `vector` instances were constructed and subsequently not accessed (or resized prior to first access). – al45tair Feb 26 '14 at 19:02