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?
Max size for std::vector
-
- Posts: 1725
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: Max size for std::vector
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.why does it happen?
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.
Who is online
Users browsing this forum: No registered users and 469 guests