0

I want to make a char[1 048 576][16 384][1024] and a int [1 048 576][16 384] but I get an Error!

I want to make an 3d array as big as excel can handle!

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3724530
  • 53
  • 1
  • 5
  • 5
    You most certainly don't want to do that. If you could create such an array, it would take up 16TB of RAM. The `int` array would take anywhere from 64GB to 128GB of RAM depending on the size of an `int` on your machine. – tangrs Jun 12 '14 at 09:59
  • 1
    I think you're better off managing this by growing the arrays as they are needed by using functions like `malloc`, `realloc` and `free`. – tangrs Jun 12 '14 at 10:03
  • Whath that teaches you is that Excel must have a smarter memory allocation strategy than this ;-). By the way, last time I looked (must be 20 years ago), Excel could do something like 32k x 32k cells. Has that changed? – Peter - Reinstate Monica Feb 01 '16 at 21:20

2 Answers2

4

to "make" an array in C you need to allocate some memory, either statically or dynamically with malloc. In any case, the array will be mapped physically in your computer like in the RAM. So you need to have enough physical place to "make" it.

In your case, you want a char array of size: 1048576*16384*1024*sizeof(char) = 1048576*16384 MByte with char size is 1 byte.

That is just too much. The error you get is related to that. It tells you that the maximum size you can request is the max number for a signed integer. See: What is the maximum value for an int32?. which is way under what you want to allocate.

If you look at excel, the cells are all empty and no memory is reserved for them until they are filled.

Community
  • 1
  • 1
toine
  • 1,946
  • 18
  • 24
3

Unless that array is a global variable, it would be allocated on the stack, whose size is limited (usually, 1MB for VC).

Large objects should be allocated on the heap with malloc. However, such a huge (16TB approximately) object will never fit in the memory of any ordinary device. Chances are that you don't actually need all that memory at once. You should rewrite your algorithm.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80