Re: Static vector
On 30 Sep., 19:43, MC <manan.cho...@gmail.com> wrote:
I have a question about static objects.
#include <iostream>
#include <vector>
using namespace std;
void f(){
static vector<int> v;
v.push_back(1);
cout << v.size() << endl;
}
int main(){
for(int i = 0; i < 1000; i++)
{
f();
}
}
where actually does the compiler stores the vector<int> v?
The local variable v in f() has "static storage duration".
It remains unspecified, how the implementation get's it,
but it shall remain valid from program initiation until
program end.
And after every internal resize does the memory taken
by static vector grows.
This is correct. The internals of the vector manages
the memory for it's values - this memory is retrieved
from it's allocator, not that the complete specification
of the template is:
template<class T, class A = allocator<T>>
class vector;
The template std::allocator is required to obtain
it's memory from ::operator new(std::size_t). This
arena is called the free store.
Or does the compiler only stores the pointer to
vector<int> v.
The static storage will contain a complete object
of type std::vector<T>. It typically contains three
members which are pointers to T (and the allocator,
if this is a non-empty class).
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]