daviddanut
daviddanut

Reputation: 49

C Program crashing with large arrays

I'm trying to run a simulation that involves a large amount of calculations and values.

I've got an issue in that large arrays cause the program to crash before it's even declared any variables. I'm not sure if this is due to my code or due to my operating system refusing to run the program.

Code that crashes the program is:

long double adsorption[2][4][5][10001]

This array should use about 10^6 bytes, and SIZE_MAX is 10^19, so I'm confused as to why it's crashing. (Program is 64 bit in Task Manager)

I'm running on Windows 7 64bit with 12 Gb of memory, of which 10 Gb is generally free. Compiler is MinGW-w64

Apologies if this is quite basic, I'm from a physics background, not coding!

Cheers

Upvotes: 1

Views: 2379

Answers (4)

James
James

Reputation: 1019

It's a stack overflow.

Bearing in mind that;

1 byte =    n bits
1 kb   = 1024 bytes = 2^10 bytes
1 mb   = 1024 kb    = 2^20 bytes
1 gb   = 1024 mb    = 2^30 bytes
1 tb   = 1024 gb    = 2^40 bytes

Assuming the default stack sizes in gcc are (according to this webpage);

  • Linux: 1.8MB | 1,872KB | 1,916,928 bytes

  • Windows: 1MB | 1,024KB | 1,048,576 bytes


Expanding on rohit's answer (originally i was just going to write a comment, but it's to much writing for a comment and i have a solution at the end), if your array has 3,200,320 bytes | 3,125KB | 3MB

the size of your stack overflow on each platform would be;

  • Linux: 3,125KB - 1,872KB = 1,253KB | 1,283,072 bytes overflowed.

  • Windows: 3,125KB - 1,024KB = 2,101KB | 2,151,424 bytes overflowed.


To make gcc compile your program with a larger stack, like 8MB you can do;

gcc file.c -Wl,--stack,8388608

And the program should not overflow because after the array is put on the stack you are left with:

  • 8,388,608 - 3,200,320 = 5,188,288 bytes | 5,066KB | 4.9MB of stack space.

Upvotes: 1

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37944

Local variables implicitely have automatic storage duration, so they are effectively placed in space-limited stack. Either declare you array with static qualifier, move it into global scope, outside any function (so now implicitely it has static storage duration, which variables are placed on data segment, note static in this context means something totally different, so don't be "explicit to much" there), or use malloc or calloc functions from <stdlib.h> header to allocate it on heap (especially useful if size of array indices is known run-time).

Note that C99 also features variable-length arrays (VLA), where size of indices is determined run-time, however these are only limited to automatic storage duration.

Upvotes: 0

rohit
rohit

Reputation: 228

2*4*5*10001 = 400,040 elements. Double means 8 bytes per element. And 400,040 * 8 = 3,200,320 bytes

3,200,320 bytes size is beyond the stack memory size to handle.

Upvotes: 0

Aniket Inge
Aniket Inge

Reputation: 25725

adsorption is being allocated on the stack, and it must be overflowing the stack. Hence the error.

Use malloc and family to allocate large chunks of data on the heap.

edit

Or make it static -- @Matt McNabb thanks! :-)

Upvotes: 2

Related Questions