arrays !!

Jatsui's Avatar, Join Date: Feb 2008
Go4Expert Member
Hello ppl,

Does anyone know the largest 2D array possible in `C`.
So if there is arr[a][b], what are the limitations on the values of a and b ??

Cheers guys, appreciate it as always !!
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Depends on the platform. a and b are integers, so presumably the maximum allowed by the language is 2^32-1. But (2^32-1)^2 is a LOT of memory, 16 exabytes to be precise, so the chances are you're limited by RAM first, then secondly stack or heap space depending how you declare it.
Jatsui's Avatar, Join Date: Feb 2008
Go4Expert Member
So I take it that I need to check the following for array size restrictions..
1) RAM
2) Stack/heap space depending on declaration


Can you explain a bit about point 2) - What do you exactly mean by how I declare it ??


Thanks again.
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Code:
int main()
{
  int *arr1=malloc(100*sizeof(int));
  int arr2[10][10];
  return 0;
}
arr1 will be on the heap, arr2 on the stack. How you declare variables can determine where in memory they are kept. There are different types of memory; local variables go on the stack and are overwritten as functions call/return, globals go in a different type of memory (BSS I think), and malloc/new'd memory is taken off the heap.

So if you have a small stack and a large heap you will need to malloc large arrays. Conversely if you have a large stack and a small heap then you can plonk the array on the stack, provided that you don't want to keep it after the function has returned.