0

I try running this C++ code as a Windows console application, but when it starts up, it immediately closes, only giving me a standard "this program has stopped working" error that Windows usually gives me.

The main function of the program is (with a bit of extra code omitted after it):

int main() {
    cout << "FIRST LINE";
    int fract[501][501];
    int rfract[501][501];
    int nufract[501][501];
    int nurfract[501][501];
    int snufract[501][501];
    int snurfract[501][501];
    system("PAUSE");
    return 0;
}

Interestingly enough, if I only have the first 2 arrays, the program runs fine, but if I have any of the other 4 arrays (even just one of them), the program crashes upon start. I am using a computer with 4 GB of RAM, and I haven't been getting any helpful error messages, although it seems like that's the problem. If I change the sizes of all of them to [101][101] (goes from ~1.2 MB to ~50kB) the issue is solved, but I have less space to input functions into them. Is there a way to work around this?

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
Max Candocia
  • 4,294
  • 35
  • 58

3 Answers3

3

The size of the stack is MUCH smaller than the size of the heap. Anything allocated with new or malloc goes on the heap, but you can fill up the stack very very quickly if you're not careful.

Fitting given the website, you're probably getting a Stack Overflow with so many large arrays.

And to do this, try this code below:

#include <iostream>

int main(int argc, char* argv[])
{
int sarr2d[5][10];
typedef int dimension[10];
dimension* arr2d = new dimension[5];

for(int i = 0; i < 5; i++)
{
    for(int j = 0; j < 10; j++)
    {
        sarr2d[i][j] = i*j;
    }
}

for(int i = 0; i < 5; i++)
{
    for(int j = 0; j < 10; j++)
    {
        arr2d[i][j] = i*j;
    }
}

// print it out
for(int i = 0; i < 5; i++)
{
    for(int j = 0; j < 10; j++)
    {
        std::cout << arr2d[i][j] << " ";
    }
    std::cout << "\n";
}

delete [] arr2d;
return 0;
}

This is inspired by the question C++ Multi-dimensional Arrays on the Heap

Community
  • 1
  • 1
Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54
1

The stack size is usually limited to a rather small default size. If you need a larger stack, you can generally tell that to the linker (in a system specific way).

In this particular case, you can also choose to allocate the variables in static memory space outside of the main function. That space is generally much larger than the stack space.

int fract[501][501];
int rfract[501][501];
int nufract[501][501];
int nurfract[501][501];
int snufract[501][501];
int snurfract[501][501];

int main() {
    cout << "FIRST LINE";

    system("PAUSE");
    return 0;
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

If you really want the items on the stack, you can use the /F switch if you are using a Microsoft compiler, or you may be able to specify the size of the stack when you link your program. I would probably go with the /F switch. However, this may take away from the heap space (limit on both stack and heap together).

Here is a link on MSDN about increasing the stack size:

http://social.msdn.microsoft.com/Forums/da-DK/Vsexpressvc/thread/2a5b32b6-683b-4729-92d3-45ed7a89ef3f

Glenn
  • 1,169
  • 1
  • 14
  • 23