0

I'm new programming and I have found a problem I'm defining a c++ array like this

double name[512][512]

But when I run it on Windows(it compiles without errors) it crashes. When I run on Linux(Ubuntu) it runs how it should do it without problems. I think Windows is restricting the memory my program can take, am I right? How can I solve it? Thanks to everyone who can give me a hand.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458

3 Answers3

6

I'm guessing (since you did not provide an MCVE) that your name array is an automatic variable, so it gets allocated on the call stack.

BTW the problem is not in the RAM (which is managed by the operating system; user-space programs don't use directly RAM but virtual memory) but in the virtual address space (its stack segment) of your process. On Linux, you could use /proc/ (see proc(5) and pmap(1)) to query your process' virtual address space. And read Operating Systems: Three Easy Pieces to understand the role of the OS.

Notice that on x86-64 sizeof(name) is probably 2097152 bytes.

The call stack is limited in size. On Linux, the usual limit is 4 or 8 megabytes (but there is a way to change it), on Windows it is rumored to be 1 megabyte. You've got a stack overflow.

You are likely above that limit. Consider using some dynamic memory allocation (e.g. with new; however by using higher level C++ constructs such as containers and smart pointers you should generally avoid explicitly using it). Most C++ standard containers, notably std::vector (but not std::array) use it (the heap) for internal data.

As a rule of thumb, each call frame should be rather small (e.g. kilobytes).

And the compiler could have warned you, if you compile with g++ -Wall -Wextra -Wstack-usage=1500 -g on Linux. Learn also to use the gdb debugger. Be afraid of undefined behavior.

You could also use some extra library like Boost (or something else) providing matrixes, or have your own Matrix abstract data type (which would heap-allocate its data). Be aware of the rule of 5

Take several weeks to read some good C++ programming book, but be aware that C++ is a very difficult and complex programming language (so read also SICP to learn more general programming concepts and play with some Scheme implementation such as Racket). Later, you could read some book on Garbage collection (the concepts there are relevant to memory management).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

You are probably trying to allocate the array on the stack, and the stack size is smaller on the Windows system. If that is the case, make the array global or static and the problem will go away.

Another option is to allocate the array dynamically. For example:

struct Array {
    double vals[512][512];
};

// in a function:
auto name_guard = std::make_unique<Array>();
auto &name = name_guard->vals;

Here name_guard ensures that the storage will be deleted when no longer needed (as determined by the life-time of name_guard), and name has the same meaning as name in the question. Note that name must not be allowed to outlive the guard object.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
0

You are hitting stack overflow.

sizeof(double) is 8 on Windows.

Thus, 8*512*512 is 2MB. If I'm not mistaken, the default stack size for the Visual Studio compiler is 1MB. The /STACK compiler switch on the linker can be used to increase this size.

As others are likely to point out, for large arrays, use the heap (malloc/new, free/delete) instead of taking up large amounts of stack with array variables.

selbie
  • 100,020
  • 15
  • 103
  • 173