Is there any limit as to how many elements a 2D integer array
can contain in C
?
PS : I was expecting there would be some space limitations in declaring an array but could not find any such reference in the internet.
-
1The array cannot exceed `SIZE_MAX` bytes (this constant is defined in `limits.h`). Particular systems might have smaller limits depending on how much memory is available. – M.M Sep 07 '16 at 04:39
-
possible duplicate of http://stackoverflow.com/questions/216259/is-there-a-max-array-length-limit-in-c – Rishikesh Raje Sep 07 '16 at 04:40
-
1@RishikeshRaje that's a C++ question – M.M Sep 07 '16 at 04:40
3 Answers
It depends on your RAM
or the memory available for you.
i:e:
My program used to crash when I declared a global array a[100000][10000]
, but this declaration is fine with the system now I have.

- 1,036
- 1
- 11
- 31
-
2This isn't really true. For example, on 32 bit Windows you couldn't allocate more than 4,29Gb even if your computer had more RAM than that. Anyway, C provides the `size_t` type as a way to find out how much memory you can allocate in one object. – Lundin Sep 07 '16 at 06:30
-
The size_t
type is defined to be large enough to contain the size of any object in the program, including arrays. So the largest possible array size can be described as 2^(8*sizeof(size_t)
bytes.
For convenience, this value can be obtained through the SIZE_MAX
constant in limits.h. It is guaranteed to be at least 65535 but is realistically a larger value, most likely 2^32 on 32 bit systems and 2^64 on 64 bit systems.

- 195,001
- 40
- 254
- 396
Maximum imposed by the C/C++ standard: x * y * z <= SIZE_MAX
, where SIZE_MAX is implementation defined, x is one dimension of the array, y is the other dimension, and z is the size of the element in bytes. e.g. element_t A[x][y]
, z = sizeof(element_t)
.

- 134
- 8