Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"virtual memory exhausted: Cannot allocate memory" only when compiling with debug flag

I get the error with following code (error.cpp):

#include <map>
#include <functional>
#include <vector>

int main()
{
    std::map<
        int, std::map< std::string,
        std::map<std::string, std::map<std::string, std::map<std::string,
        std::map<std::string, std::map<std::string, std::map<std::string,
        std::map<std::string, std::map<std::string, std::map<std::string,
        std::map<std::string, std::map<std::string, std::map<std::string,
        std::map<std::string, std::map<std::string, std::map<std::string,
        std::map<std::string, std::map<std::string, std::map<std::string,
        std::map<std::string, std::string>
            > > >
            > > >
            > > >
            > > >
            > > >
            > > >
            > >                                         oups;

}

When compiling with debug flag:

g++ error.cpp -g -o error

My system is Ubuntu 18.04 with g++ 7.5.0 running as a VM. RAM is 5GB and Swap is 2.5GB. Hard disk space left 1GB.

Is this normal? A bug? A limitation?

What would be a "nicer" alternative for above code ? (c++14)

like image 519
jav Avatar asked Nov 24 '25 10:11

jav


1 Answers

You have 8+7+6 strings in that definition, right? So, I'd say what you have on your hands is a relation of arity 21. Why not try:

constexpr const std::size_t my_arity = 21;
std::unordered_set<std::array<std::string, my_arity>> oups;

instead?

like image 121
einpoklum Avatar answered Nov 27 '25 00:11

einpoklum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!