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?

BlueTune
  • 905
  • 4
  • 14

2 Answers2

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.

Bathsheba
  • 227,678
  • 33
  • 352
  • 470
-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
}
Jean-Marc Volle
  • 2,813
  • 1
  • 13
  • 18
  • Global variables are neither stored in the stack or heap. See [SO](https://stackoverflow.com/questions/44359953/are-global-variables-in-c-stored-on-the-stack-heap-or-neither-of-them) – BlueTune Apr 21 '20 at 08:09
  • Do bear in mind that `main` cannot be called from itself (either directly or indirectly), so I think your `if` statement is unnecessary. Unless you're thinking in terms of guarding against a fork? – Bathsheba Apr 21 '20 at 08:10
  • i dont agree that it is a "bad idea". If it fits, I would prefer `std::array` over `std::vector` when possible – 463035818_is_not_a_number Apr 21 '20 at 08:11
  • @Bathsheba You are right, with few knowledge about how the global variable will be used I showed how to ensure it is allocated only once but it's useless in the context of the example. – Jean-Marc Volle Apr 21 '20 at 08:19
  • @BlueTune Thanks for the clarification I forgot about BSS. I corrected my answer. – Jean-Marc Volle Apr 21 '20 at 08:22
  • @ idclev Can you clarify why you prefer std::array? The only reason I see is for embedded systems with sparse memory where you want to know at compilation time if your code memory needs are not bigger than available. – Jean-Marc Volle Apr 21 '20 at 08:25
  • @Jean-MarcVolle because imho using dynamic allocations as the default is a "bad idea" ;). Also for semantics, if I want a fixed size array that is `std::array` not `std::vector` – 463035818_is_not_a_number Apr 21 '20 at 08:51