Work with stl: list and stl :: vector strings in interrupt handlers. I want to avoid calls to malloc ().
Question: what is the best way to prevent malloc () calls in the STL list and vector? Is it enough to create a structure with a predetermined size and then avoid push / pop / erase calls?
Thank you in advance
As a recommendation: we use two methods mentioned in other answers in my workplace:
, , , .. (, , ) .
, , :
, , , , . , .
- : , , -.
STL, std::list std::vector, , Allocator. , , . , , ( , STL ).
std::list
std::vector
Allocator
20.1.6 ++ standard
, , . , , , , .
std::vector, reserve(). push_back(), pop(), insert() erase() ( , ). ( , ), . reserve(x) , x, . ( , , - , swap(), erase(), .)
reserve()
push_back()
pop()
insert()
erase()
reserve(x)
x
swap()
std::list, , : , "" . , , splice(), "" "" . splice(), "" "" .
splice()
: a const std::vector . , , const, vector .
const std::vector
const
vector
onebyone.livejournal.com, , ++ . , . , , C/++, sig_atomic_t , :
sig_atomic_t
sig_atomic_t flag = 0; // This signal handler has well-defined behavior void my_signal_handler(int signum) { flag = 1; } int main(void) { signal(SIGINT, &my_signal_handler); while(1) { doStuff(); if(flag) { flag = 0; actuallyHandleSignalNow(); } } return 0; }
, .
To std::vectordo this should be enough. However, I do not think that this guarantees anything. Memory allocation is seen as an implementation detail. If you can limit yourself to a specific size, I suggest using a simple static array. This way, you have small-scale control over what exactly is happening.
Source: https://habr.com/ru/post/1712991/More articles:Does / abcd ^ $ / i have anything in Perl? - regexКак выделить текст в TextArea - javascriptRead the html page in asp.net mvc - asp.netHow to protect object fields in Rebol? - rebolcleaning when using exceptions and files in python - pythonPHP classes and including files - includeSilverlight 3 - 3D Engine - silverlightMultithreaded search operation - javaAlgorithm for determining weak / good / strong password - javascriptRails 2.3.2 пытается отобразить ERB вместо HAML - ruby-on-railsAll Articles