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

1

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?

c++
visual-c++
global-variables
asked on Stack Overflow Apr 21, 2020 by BlueTune

2 Answers

3

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.

answered on Stack Overflow Apr 21, 2020 by Bathsheba
-1

According to Does std::array<> guarantee allocation on the stack only?

std::array is allocated on the stack, not the heap so it is a bad idea to use it if you need a big chunk of memory

I would use a std::vector and do dynamic allocation.

This can be done as follows:

#include<vector>

static std::vector<char> BigGlobal; 

int main()
{
   // one time init: can be done anywhere.
   if (BigGlobal.empty())
   {
       BigGlobal.resize(1000000000);
   }

    //Do stuff with BigGlobal
}
answered on Stack Overflow Apr 21, 2020 by Jean-Marc Volle • edited Apr 21, 2020 by Jean-Marc Volle

User contributions licensed under CC BY-SA 3.0