The following is probably not platform-dependent, but I'll fix it at Win 10 GCC in any case.
Suppose you create an array or vector, consisting of elements that each can be variable-sized, in static memory space in main():
RADIAL_UNITS = 1000000; static vector<Pearl> necklace[RADIAL_UNITS] = { }; //each element is a variable-sized vector, which can consist of anywhere from 1-50 Pearl objects or allocated on the stack in main() (assuming the stack space is set to allow at least 1000000 memory addresses):
vector<Pearl> necklace[RADIAL_UNITS] = { }; I'm assuming that, at runtime, necklace consists of RADIAL_UNITS contiguous memory addresses, pointing to the vector<Pearl> elements. What's not clear to me is in (i) which memory space the vector elements reside (I'm suspecting the heap space).
Side questions I'm interested in as well:
It's also not clear to me (ii) how the compiler knows that the elements are variable sized. Do the STL containers have something defined internally that communicates this? If they were fixed sized, I'm assuming they would literally be contiguously present in whatever region we allocated the array to (the second case, i.e., allocation on the stack , would probably result in a segfault unless the default stack space in enlarged). (iii) Is it possible for me to fix the vector elements at size 50*sizeof(Pearl) in order to allocate them in the same memory space the array was originally defined as? Pearl types consist of two flating point numbers, so they are of fixed size.
necklacewill be a contiguous memory area, but not of pointers but ofRADIAL_UNITSnumber ofstd::vectorinstances. The size (in bytes) of the array will beRADIAL_UNITS * sizeof(std::vector). Note thatsizeof(std::vector)is not related to the number of elements in the vector.