c++ system() raises ENOMEM

后端 未结 2 778
礼貌的吻别
礼貌的吻别 2021-01-28 23:27

This question is a M(not)WE of this question. I wrote a code that reproduces the error:

#include 
#include 
#include 

        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-28 23:50

    Your line

       std::vector a(7e8,1);
    

    is probably wrong. You are calling a constructor for std::vector which takes a vector size and an initializing element. The 7e8 is converted to a huge size (i.e. to 700000000 elements).

    You might want to construct a two-element vector, so use

       std::vector a{7e8,1};
    

    And with your huge vector, system(3) library function will call fork(2) system call which is failing with:

    ENOMEM fork() failed to allocate the necessary kernel structures because memory is tight.

    Maybe you reached some limit, e.g. set by setrlimit(2) somewhere else.
    Try cat /proc/self/limits to find them (on Linux).

    Use strace(1) (e.g. as strace -f yourprogram) to find out what is happening; look around the fork or clone line...

    BTW, system(3) should return an error code on failure. You should test it. And you might want to call system("echo here pid $$"); instead of system(NULL);

提交回复
热议问题