Hello, is there any way I could find the maximum size of std::vector that I can allocate at the moment? I'm building a device that collects a lot of data in short period of time and I need some way to temporarily store it in RAM of my embedded chip. Since some things are dynamically allocated based on connected sensors, I can't know in advance how much space I'll have left. Can I somehow find the maximum vector length at runtime?
#Maximum allocation size
23 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
and if you don't have enough space left?
This doesn't really solve the problem, since with vectors I don't need to use new and delete (also, they're considered bad practice in modern cpp). And even if I used new to create an array, I don't know if it is possible to allocate the required amount of memory in advance. The only thing I can do is trying several values and finding the biggest array size for which it doesn't fail and that's not a nice solution at all
That's the thing I'm worried about and that's the point of this question. How do I know how much space in RAM I have left?
For context, my device only has 256KB of RAM
it's complicated. that's why I'm asking what you'd actually be doing with that information.
allocate less?
Sometimes for smaller systems you might not want to use something like a vector
Because things like vectors may fragment your heap (turn it into Swiss cheese)
256 kb isn’t too bad, but you might want to consider allocating a large buffer on the stack instead
Heap fragmentation basically leads to you unable to allocate large chunks of continuous memory (such as in a vector)
A deque-like type (linked list of small arrays) may also work
why would vector fragment heap? it's linear memory isn't it?
unless it's just because that's the nature of small embedded hardware? idk, I don't work with that kind of stuff
I guess I could understand if you allocate 200kb, and you want to push, and vector needs to reallocate, there's literally not enough memory left for vector to get a brand new chunk, is that what you're talking about?
Heap allocation + (somewhat) frequent reallocation
Kinda yeah, but there’s also the issue of previous vectors leaving behind a bunch of separate small “pockets” of free memory due to the reallocations
Thank you, I'll try to find some way to allocate it statically. It seems like the best option, if I can find a clever way to organize my data
Another reason not to sleep tonight 🙃
@versed flower Has your question been resolved? If so, run !solved :)
!solved