Std :: list, std :: vector and malloc ()

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

+3
source share
6 answers

As a recommendation: we use two methods mentioned in other answers in my workplace:

  • : , / "" (., , boost:: pool) Allocator - std:: set std:: map, std:: list.
  • / : std::vectors, , ( , ) .

, , , .. (, , ) .

, , :

  • /: -, ; ( ) , STL. - , , .
  • alloc/free: , /, - , . / (, , std:: list manipulation) interrupt-handler-as-manufacturer, all-else-as-consumer, . - std:: list (, , ), , , .

, , , , . , .

- : , , -.

+4

STL, std::list std::vector, , Allocator. , , . , , ( , STL ).

20.1.6 ++ standard

+14

, , . , , , , .

std::vector, reserve(). push_back(), pop(), insert() erase() ( , ). ( , ), . reserve(x) , x, . ( , , - , swap(), erase(), .)

std::list, , : , "" . , , splice(), "" "" . splice(), "" "" .

+7

: a const std::vector . , , const, vector .

+3

onebyone.livejournal.com, , ++ . , . , , C/++, 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;
}

, .

+2

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.

+1
source

Source: https://habr.com/ru/post/1712991/


All Articles