3

I have approx. 2 GB of free DRAM on my computer. Compiling either a std::array or the standard array:

#include <iostream>
#include <array>

int main(int argc, char *argv[]){

    // int* a = new int[500000000];
    std::array<int, 2000000> a;

}

with:

$ g++ -std=c++11 main.cpp -o main
./main

works for both arrays. Changing the size of the std::array to:

// ceteris paribus 
std::array<int, 2095300> a; 

leads to:

$ ./main
Segmentation fault (core dumped) 

honestly, I am not sure whether or not this issue has already been addressed somewhere.

From my understanding, the std::array is created on the stack, and the int * ... array on the heap. Now my guess was that maybe my stack is simply not larger then the ~8mb, which compared to the 2 GB heap sounded disproportionate. Thus I also tried out:

//int a[2096000];

which also causes a segmentation fault. So my question is, what causes the Segmentation fault?

Thank you in advance.

Vincent
  • 1,361
  • 2
  • 20
  • 33
  • 2
    most likely a stack overflow, use a vector. – Borgleader Oct 31 '14 at 17:59
  • 1
    stackoverlow, increase stack size for your program. – Mehmet Fide Oct 31 '14 at 18:00
  • 4
    You may have 2 GB RAM on your machine, but your available stack size is certainly smaller. – πάντα ῥεῖ Oct 31 '14 at 18:01
  • Right, however I intended to use the array as a container in another class where the size of the container is known at compile time. Thus it suggested to use the std::array however it seems that it's not able to store larger data sets. – Vincent Oct 31 '14 at 18:01
  • 2
    An `std::array` on the stack isn't appropriate for large amounts of data, I'm afraid. – Neil Kirk Oct 31 '14 at 18:04
  • 2
    You can still use `std::array* a = new std::array();`, though I wouldn't recommend it (in favor of an appropriately (pre-)sized `std::vector`). – πάντα ῥεῖ Oct 31 '14 at 18:05
  • 2
    @Vincent you can allocate it on the heap, either directly as @πάντα ῥεῖ suggested or wrapped in a smart pointer of some kind, like `unique_ptr` or `shared_ptr` – PeterT Oct 31 '14 at 18:06
  • @all: alright thank you guys I really appreciate your quick replies. It just sounded a bit disproportionate that the stack is so much smaller then the heap. I will follow your recomendations and stay with the vector. Thanks again – Vincent Oct 31 '14 at 18:10
  • @Vincent it should also be noted that declaring `std::array a;` outside of a function, either globally, in a namespace or as a static data member of a class/struct will also not allocate it on stack. – PeterT Oct 31 '14 at 18:13
  • @PeterT: well actually it's supposed to be the container for data of a matrix class, thus I am not sure if this is would be a proper solution. – Vincent Oct 31 '14 at 18:16
  • +1 for asking a question about a stack overflow bug on stackoverflow. – Mark Beckwith Nov 01 '14 at 01:53

1 Answers1

5

You're putting a large array on the stack, causing the stack to overflow.

You can set how large the stack is: Change stack size for a C++ application in Linux during compilation with GNU compiler. A better option is probably to use the heap, however.

It just sounded a bit disproportionate that the stack is so much smaller then [sic] the heap.

The stack is memory that's actually allocated, which means you don't want it to be larger than you really need, because if memory is used for the stack then it won't be available for other uses. The heap, on the other hand, doesn't take up memory unless it's actually requested, so allowing the heap to potentially take up a large proportion of the address space is fine.

The stack also generally doesn't need to be very large because the maximum depth of function calls usually isn't that high. A few megabytes is almost always more than enough.

Community
  • 1
  • 1
bames53
  • 86,085
  • 15
  • 179
  • 244
  • The stack need not be already allocated, the memory space only need be reserved: that is slightly different on some systems. The stack was made small because historically, memory space is actually an expensive commodity, especially in a multi-threaded system. However, on 64 bit systems with a few gigs or terrabytes of ram, it is no longer an expensive commodity. – Yakk - Adam Nevraumont Oct 31 '14 at 19:52