#Maximum allocation size

23 messages · Page 1 of 1 (latest)

versed flower
#

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?

pallid topazBOT
#

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.

flat olive
versed flower
# tight summit https://www.learncpp.com/cpp-tutorial/dynamic-memory-allocation-with-new-and-del...

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

versed flower
#

For context, my device only has 256KB of RAM

flat olive
#

allocate less?

grim grotto
#

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

rancid latch
#

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?

grim grotto
grim grotto
versed flower
#

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 🙃

pallid topazBOT
#

@versed flower Has your question been resolved? If so, run !solved :)

versed flower
#

!solved