Page 1 of 1

Max size for std::vector

Posted: Mon Sep 04, 2023 9:02 am
by brazoayeye
Hello, i'm using large std::vector in my code. In some rare eventualities one of them grow larger than 32k.

My code crashes when i perform a push_back in a vector<u8> that has 32768 elements. If i catch the exception I have std::bad_alloc and it's like libsupc++/new_op.cc @ line 54

I have esp_get_free_heap_size() ~ 87k

I can ofc make a workaround to keep vectors smaller, but is there a quicker solution and why does it happen?

Re: Max size for std::vector

Posted: Mon Sep 04, 2023 11:44 am
by MicroController
why does it happen?
Heap fragmentation, plus the need to temporarily hold memory for both the old and the new data array during resizing, plus the implementation of vector potentially implicitly reserving more memory than required to reduce the number of future reallocations.
You can mitigate the issue by using std::vector::reserve() to avoid as many reallocations as possible.

Generally, I would advise to avoid std::vector, as well as std::string, on embedded targets due to their need for frequent allocation and deallocation of heap memory, promoting heap fragmentation.