Max size for std::vector

brazoayeye
Posts: 41
Joined: Sun May 03, 2020 2:37 pm

Max size for std::vector

Postby brazoayeye » Mon Sep 04, 2023 9:02 am

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?

MicroController
Posts: 1552
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Max size for std::vector

Postby MicroController » Mon Sep 04, 2023 11:44 am

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.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 302 guests