19

If std::vector and friends are self resizing, does that mean if I declare a vector like so:

std::vector<string> myvec;

Then it'll resize using more stack, whereas:

std::vector<string> *myvec = new std::vector<string>();

Would resize using more heap?

quamrana
  • 37,849
  • 12
  • 53
  • 71
Benj
  • 31,668
  • 17
  • 78
  • 127
  • 2
    You may also implement a 'fixed-size' vector to store all this on the stack. It works quite well and may be an optimization. However, the cost is that it won't be infinitely extensible > you'll have to settle for a high-limit, and it will always consume that much space. – Matthieu M. Oct 29 '09 at 11:02
  • That's interesting, so a "fixed-size" vector is entirely on the stack? – Benj Oct 29 '09 at 11:05
  • 1
    I don't know a 'fixed-size' vector, but if you want your fixed data on the stack, consider using std::tr1::array, which seems similar. – stefaanv Oct 29 '09 at 11:54
  • @Benj: No. A vector object has a fixed size (no matter how much you put into it). But the vector (probably) contains a pointer to a heap allocated chunk of storage where it keeps all the objects placed inside the vector. – Martin York Oct 29 '09 at 19:28
  • What is the purpose of second option? – Omkar Somani Apr 23 '21 at 14:31

3 Answers3

26

Vectors allocate on the heap in their internals.

The only thing you pay for in the stack for a stack based bector is a couple of bytes, the inner buffer will always be allocated from the heap.

So effectively when you do a vec = new vector() you are allocating a small quantity, which may not be really good.

Arkaitz Jimenez
  • 22,500
  • 11
  • 75
  • 105
  • Ah ok, so do vectors declared on the stack delete the memory they've used once they go out of scope then? – Benj Oct 29 '09 at 11:03
  • @Benj: Yes, they will cleanup the memory when they go out of scope. – Naveen Oct 29 '09 at 11:04
  • 7
    @Benj: do note that vectors only clean up the memory they allocated themselves. If you add heap allocated objects (with 'new') to a vector, then you are responsible for calling 'delete' on them. Maybe you already knew this, but I wanted to mention it just in case... – StackedCrooked Oct 29 '09 at 17:25
9

In the first case, you are creating the vector on stack. That doesn't mean that all the vectors internal objects are on stack as well. In fact, vector will still allocate the memory required to hold the objects on heap only. This is because, to allocate on stack you should know how many objects to create. But this information is not available, so the only remaining option is to allocate the memory for the contained object from heap.

Naveen
  • 74,600
  • 47
  • 176
  • 233
  • It should also be noted that it's common for std::string to keep some of it's internal "objects" on the stack. Actually, is std::vector prohibited from doing that? – Mooing Duck Sep 12 '11 at 23:41
2

std::vector always has its buffer allocated on heap. So regardless of where the vector itself is allocated resizing it will only affect the heap.

sharptooth
  • 167,383
  • 100
  • 513
  • 979