Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   arrays !! (http://www.go4expert.com/forums/arrays-t14652/)

Jatsui 21Oct2008 01:41

arrays !!
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 21Oct2008 21:58

Re: arrays !!
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 22Oct2008 00:35

Re: arrays !!
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 22Oct2008 03:32

Re: arrays !!

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.

All times are GMT +5.5. The time now is 17:25.