views:

228

answers:

2

Hello,

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.

+2  A: 

Make the array dimension an unsigned long long.

unsigned char sram[12884901888ULL];
laalto
Depends on the compiler. If it converts the length to a size_t first then it will not work.
finnw
+2  A: 

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.

finnw
@finnw: Can u kindly explain what is the macro sram, that u mentioned is trying to declare? Some pointer to an array, ...?
goldenmean
It's an array (not a pointer to one.)
finnw