-2

I am making a 3D array program in C.

When I enter [31][11][99] the code runs fine.

But when I enter [31][11][999] the code is not run.

Is it because of the array size ??

If that is the reason.. then what would be the maximum array size for a 3D array program in C.

sam
  • 2,426
  • 2
  • 20
  • 29
  • you REALLY need to give us a little bit more info. There is no maximum, and is is not because of the array size, whatever "it" is. Does it compile? – user1338 Nov 13 '13 at 11:52
  • We need to see the code to be able to comment. – Oliver Matthews Nov 13 '13 at 11:55
  • check the max stack size of your system and see if your array get over it – Davide Berra Nov 13 '13 at 11:57
  • Storage limitation on automatic variables (stack allocated) is implementation specific. In theory, even something as small as `int arr[10];` could fail to allocate memory.But in practice with reasonable knowledge of stack frame size, you should be fine. The problem with larger stack allocation is that there's no easy way to detect allocation failures.If you even think that you might exceed stack limit, you probably should allocate memory dynamically. Related:[Why does C not define minimum size for an array?](http://stackoverflow.com/questions/14695254/) – P.P Nov 13 '13 at 12:04
  • No limitation there in array – Just code Nov 13 '13 at 12:14

2 Answers2

1

It depends on where you allocate the array. If you allocate it locally/statically, it depends on the available stack size. How much stack memory that is given to a processes depends on the specific operative system.

If you run out of stack space, you get stack overflow, in runtime. Therefore you should always allocate large arrays like these dynamically, on the heap.

Lundin
  • 195,001
  • 40
  • 254
  • 396
1

It is most likely that you run out of stack space. You can find out the size of the stack on Linux using ulimit -s what gives you the size of the stack in KiB. Your array would contain 340659 elements. Assuming 4 byte per element gives 1362636 bytes for the array. The stack size on my machine is

$ ulimit -s
8192   # KiB

Your first example gives 135036 KiB. While it is in principal possible to resize the stack size to fit your needs, it is probably better to allocate memory for your array on the heap (Memory allocation areas in C++ (Stack vs heap vs Static)).

The way to do so is discussed here: Malloc a 3-Dimensional array in C?

Community
  • 1
  • 1
PhillipD
  • 1,797
  • 1
  • 13
  • 23