Search code examples
c++visual-c++global-variables

Declaring a big global variable in c++ results in the error message 0xc0000018


For my application I need to declare a big std::array in global memory. Its total size is about 1GB big. So I declared a global variable just like this:

#include<array>

std::array<char,1000000000> BigGlobal; 

int main()
{
    //Do stuff with BigGlobal
}

The code compiles fine. When I run the application I am getting the error message:

The application was unable to start correctly (0xc0000018). Click OK to close the application

I am using Visual Studio 2017. I am aware of the fact, that there is a MSVC Linker Option for the stack reserve size. But it is only relevant for local variables not for global variables. Can you please help me to fix the issue?


Solution

  • C++ compilers are full of limits - some make it into the standard, some don't.

    Common limits include a size limit on the length of variable names, the number of times a function can call itself (directly or indirectly), the maximum size of memory grabbed by a variable with automatic storage duration and so on.

    You've hit upon another limit with your use of std::array.

    A sensible workaround in your case could be to use a std::vector as the type for the global, then resize that vector in the first statement of main. Of course this assumes there is no use of the global variable prior to program control reaching main - if there is then put it somewhere more explicit.