Imagine a structure
struct A {
int i;
int arr[1000];
};
std::vector<A> vec;
I cannot find an answer to know if the array "arr" in my structure "A" will take up space on the stack or not at all. I these although the vector will allocate an array of "A" which will be on the heap and will not impact my stack but will my "arr" array impact my stack?
std::vector <A> vec1 (1000000);
std::vector<std::unique_ptr<A>> vec2 (1000000);
Will vec1 "destroy" my stack and better to use vec2?
Edit for the comment.
Why can vec1 allocate more elements than vec2?
#include <iostream>
#include <vector>
#include <memory>
struct A {
int i;
int arr[1000];
};
int main() {
std::vector<std::unique_ptr<A>> vec1;
std::vector<A> vec2;
std::cout << vec1.max_size() << '\n' << vec2.max_size() << '\n';
}
std::vector<A>allocates it's backing storage on the heap so all theAs in thestd::vectorwill be on the heap. The stack size (sizeof) of anystd::vector<T>is usually the same as the size of 2 pointers (implementation dependent).sizeof(std::unique_ptr<A>)vssizeof(A)- a vector is a container of "things" if the things are different sizes then you can hold more or less things in the total potentially available space. Also note the number of potential elements are stupidly large in both cases (64-bit).vector::max_sizeisn't meaningful when you are targeting a 64-bit platform. It represents filling the whole address space with elements of the vector, and you can't buy one system with that much RAM.std::vector(in contrast to short string optimization forstd::string, which is possible). There are some restrictions in the standard that disallows it. For instance, IIRC, swapping contents of two vectors may not invalidate iterators/pointer/references to their elements.