Segmentation Error: Stack Overflow

On linux 2.6.32-220.7.1.el6.x86_64 and g++ 4.4.6 , the following happens.

The following code:

 #include <iostream> #include <cstdlib> int PROB_SIZE = 10000000; using namespace std; int main(int argc, char *argv[]) { unsigned int numbers[PROB_SIZE]; cout << "Generating " << PROB_SIZE << " random numbers... " << flush; return 0; } 

Create the following SIGSEGV: (gdb) Initial program: / home / cpd20202 / sort / error

 Program received signal SIGSEGV, Segmentation fault. 0x000000000040093b in main (argc=1, argv=0x7fffffffe4f8) at error.cpp:13 13 cout << "Generating " << PROB_SIZE << " random numbers... " << flush; Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6_2.5.x86_64 libgcc-4.4.6-3.el6.x86_64 libstdc++-4.4.6-3.el6.x86_64 (gdb) where #0 0x000000000040093b in main (argc=1, argv=0x7fffffffe4f8) at error.cpp:13 

I really have no ideas.

+4
source share
5 answers

Your array of "numbers" is allocated on the stack and is probably too large. You will need to dynamically allocate the array.

+5
source

This is because your array is larger than the stack size. Thus, your program crashes when it tries to call something new during a function call.

You will get conceptually the same thing as a stack overflow, except that the local variable is incredibly large, and not too many function calls.

The stack is a small area of โ€‹โ€‹memory for use by household functions and local variables. It was never big, just a few megabytes. This is why you need dynamic allocation to get rid of your problem. Most dynamic allocations will concern the heap, which is often limited only by your physical memory.

You will need to allocate an array on the heap. You have several options for this, the simplest of which is probably using std::vector<int> . They behave in much the same way as regular arrays, and their storage is automatically controlled, so this should not be a problem.

 #include <vector> #include <iostream> int PROB_SIZE = 10000000; using namespace std; int main() { vector<int> numbers(PROB_SIZE); cout << "Generating " << PROB_SIZE << " random numbers... " << flush; return 0; } 
+8
source

There is not enough stack space in your process to allocate ten million integers. This is 40 megabytes (or 80 if your int is 64-bit), and processes usually start with a stack of about one megabyte.

You have two main options:

  • Select your array as a global variable (by moving its declaration outside of main ).
  • Allocate an array on the heap using malloc , new or std::vector .
+3
source

This is not a cout. You allocate a very large array, numbers , on the stack and insert your stack. The stack is usually 8 mb or so, where the array is 40 mb.

 int v[size]; // stack int v* = new int[size]; // heap 
0
source

You allocate too much space for processing the stack (10 million ints is a very large amount).

If you really need it, I suggest you use the empty space using:

malloc (sizeof (int) * 10000000);

0
source

Source: https://habr.com/ru/post/1412294/


All Articles