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).