Defining a large array of size larger than a unsigned int limit

Go To StackoverFlow.com

3

I need to define an array statically (in a *.h) file of size 12884901888 like.

unsigned char sram[12884901888]; //All of my code is C.

Above declaration gives error and does not work.

Because constants used in array declarations are unsigned int. But the constant i need to use (12884901888) is larger than the unsigned int limit.

How can i define the array as above, of size 12884901888 ?

Thank you.

-AD

P.S. I know many will say, optimize on that humongous array size, but i need to use same for some reason specific to my case.

2009-06-16 08:43
by goldenmean
You may want to consider using a two-dimensional array. I forget what the standard says in this case, but I'm fairly certain that (at least in C90) a fixed-size two-dimensional array is guaranteed to use a row-pointer layout. If that is the case, then the two-dimensional array can be treated as a one-dimensional array (using the row-pointer layout "formula", of course) - NoName 2009-06-17 07:38


2

Make the array dimension an unsigned long long.

unsigned char sram[12884901888ULL];
2009-06-16 08:52
by laalto
Depends on the compiler. If it converts the length to a size_t first then it will not work - finnw 2009-06-16 08:55


2

Is this for an embedded microcontroller? You can often get away with something like:

#define sram (*((unsigned char (*)[1]) 0))

Unless your compiler implements bounds checking, the array size does not matter. In any case you do not want the compiler to attempt to reserve 12884901888 bytes, because linking will fail.

2009-06-16 08:53
by finnw
@finnw: Can u kindly explain what is the macro sram, that u mentioned is trying to declare? Some pointer to an array, ... - goldenmean 2010-02-04 17:06
It's an array (not a pointer to one. - finnw 2010-02-04 17:46


0

Converting 12884901888 to hexadecimal gives : 0x3-0000-0000 (I separated each group of 16 bits)

In other words, this array of unsigned bytes needs 3 times 4 Gig The compiler is supposed to generate 34 bits address pointer for this to work

I agree with finnw, you don't need to tell the compiler the size of the array. If you do specify the size, you will get a large OBJ file for the module and similarly large ELF/EXE for the final executable.

2016-03-02 02:10
by Christian Gingras
Ads