2
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    float x[1000][1000];

    return 0;
}

I get " First-chance exception at 0x01341637 in s.exe: 0xC00000FD: Stack overflow." why?

4 Answers4

7

Your array is simply too large to fit on the stack. You don't have enough stack space for 1000 * 1000 elements.

You'll need to allocate your array on the heap. You can do this using the new keyword, but an easier way is to just use std::vector.

std::vector<std::vector<float> > floats(1000);
for (unsigned i = 0; i != floats.size(); ++i) floats[i].resize(1000);

This will give you a two-dimensional vector of floats, with 1000 elements per vector.

Also see: Segmentation fault on large array sizes

Community
  • 1
  • 1
Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • @HPT - one solution is to use dynamically allocated memory, or use something like a vector of vector. – wkl Jan 07 '11 at 20:01
  • You can also do a static array of dynamic arrays (aka an array of pointers). Just be aware that the dynamic arrays won't be contiguous in memory. – user541686 Jan 07 '11 at 20:02
  • You could also just make the array static. This should be fine. – Puppy Jan 07 '11 at 20:03
  • what is the size of stack in C++? –  Jan 07 '11 at 20:03
  • @HPT, that is platform dependent. In other words, C++ says nothing about stack-size. It depends on your OS. On some versions of Linux, for example, I think the default stack size is 8mb – Charles Salvia Jan 07 '11 at 20:04
  • @HPT: Platform/Compiler dependent. – Martin York Jan 07 '11 at 20:10
  • 1
    no need to write loop when the library does it for you: std::vector > floats(1000, std::vector(1000, 0.0)); – Gene Bushuyev Jan 07 '11 at 20:24
  • I just moved the line float x[1000][1000] outside the main function and the issue resolved. but I will mark this as answer. –  Jan 07 '11 at 20:30
  • @HPT, the reason that worked is because by moving it out of main, you are declaring it in global storage. But now you have a global variable, which is something you should generally try to avoid. – Charles Salvia Jan 07 '11 at 21:06
  • @HPT: it's generally a good idea to *understand* your code, rather than just moving stuff around until it seems to work. – jalf Jan 07 '11 at 21:13
  • @jalf: moving stuff around until it works is a respectable activity, and often leads to a higher level of understanding. – TonyK Jan 07 '11 at 22:53
  • 1
    @TonyK: not in my experience. It usually just leads to more moving stuff around until it works. Repeat until moving stuff around no longer makes it work, at which point the "programmmer" usually gives up. – jalf Jan 09 '11 at 14:43
1

float is 4 bytes, so 4 * 1000 * 1000 = 4 megabytes.

"stack size defaults to 1 MB"

See here: http://msdn.microsoft.com/en-us/library/tdkhxaks(v=VS.100).aspx

Zac
  • 3,235
  • 4
  • 25
  • 30
0

As others explained, the size of the object is bigger than the (default) size defined for function stack frame. There are two solutions: 1) create an object on the heap, which is likely to be bigger; or 2) increase the function stack frame size, which can be problematic in 32-bit environment, because you can run out of addressable space, but it can easily be done in 64-bits.

Gene Bushuyev
  • 5,512
  • 20
  • 19
-1

Just declare your array static:

static float x[1000][1000];

Edited to add:

Sigh Another silent downvoter. Not that I'm surprised. This is obviously the simplest solution to OP's problem, so it violates the prime tenet of the OOP Komissariat: The simplest solution is always wrong.

TonyK
  • 16,761
  • 4
  • 37
  • 72