Reputation: 70
I'm currently dealing with total of 21 megabytes of plain text data that is written onto several files. It consists of some strings and integers. Containing them properly is important for me to be able to loop through them in various ways so I'm using a fairly complex typedef
for my container such as
typedef std::tuple<std::string,
std::array<time_t, 12>,
std::array<std::map<std::vector<UINT>, bool>, 4>> cont;
I put my data into a std::vector<cont*> v
at final step. Is my usage logically feasible? I once read that OS reserves 1 megabyte of memory for each executable's stack memory. Is it true? Let's suppose that my data drastically increased. Pointers such as these in my system are 8 bytes by size no matter what they point to. It makes sense since they're just some sort of integers pointing to somewhere in memory and they could be considered as plain long long int
. Now assuming that stack memory is 1 megabyte and inserting 131072 pointers into this vector could fill it up since the vector itself is allocated in the stack. So what is the correct approach for such an abstract scenario? Would, allocating this vector itself too in the heap, suffice? I mean something like typedef std::vector<cont*> cont2; cont2* v=new cont2{};
Now can I access the elements as usual? How is the correct syntax for that?
Upvotes: 0
Views: 108
Reputation: 19113
Now assuming that stack memory is 1 megabyte and inserting 131072 pointers into this vector could fill it up since the vector itself is allocated in the stack.
That is false. The vector variable itself is on the stack, but not its elements. sizeof(vector)
is roughly 3 pointers, no matter what it contains. The backing array is always on the heap. So feel free to store the elements by value - std::vector<cont>
. All resizable STL containers use heap.
Overall I would not worry about stack in non-embedded environments too much. Using char buffer[256]
as a scratch space is fine. On the other hand don't put stuff on the stack just because you are worried about dynamic allocation overhead.
Upvotes: 3