I faced one issue while working on a C code with Microsoft Visual Studio-2005 compiler.
I tried to declare a big buffer statically as :
int gbl_data[4096*4096*256];
EDIT: This declaration was a global variable in a header file.
It was giving an compilation error saying – “cannot allocate an array of constant size 0”.
Means somehow the size of 4096X4096X256 was getting too large than the MAX_INT_LIMIT size (2^31) and may be wrapping around and become –ve or so. But then it should have given error as “negative subscript”.
I tried casting the constants as 4096UL x 4096UL x 256UL , still same compilation error.
What is the cause of this error?
Is it because the physical memory size falling short to allocate this large size buffer or what?
What is the fix for it?
Thanks you.
-GM.
The array size is not an int, it’s an unsigned int. An unsigned int has a max value of 4294967295. You got one more, and so it wraps around to 0.
Casting the constants as longs doesn’t change anything, because longs are also 32-bit integers on most platforms.
You could try with long longs instead, but now we run into another little problem.
You’re trying to allocate 4 billion integers. a 32-bit CPU has a memory space of 4 billion bytes. You’re trying to allocate 4 times the maximum theoretical amount of memory that can exist. (16GB)
So back to the drawing board. Figure out why you were trying to do this, and what you can do instead.