views:

96

answers:

2

Hello,

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.

+9  A: 

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.

jalf
Allocating more than 4GB is not a problem on a 32bit CPU. Available memory != Available address space. For example see http://blogs.msdn.com/oldnewthing/archive/2004/08/10/211890.aspx or http://blogs.msdn.com/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx
Joe
You have a strange definition of "is not a problem". I don't think "is not a problem" is quite the same as "can be done, with sufficient amounts of jumping through hoops. Not sure what your point is. Are you seriously suggesting that the OP should use this, rather than just fix his program?
jalf
If the OP needs to allocate 16GB, what do you suggest as a fix? If that's what he needs, it is definitely possible. I'm just contesting the incorrect statement "You're trying to allocate 4 times the maximum theoretical amount of memory that can exist."
Joe
A: 

You're attempting to statically allocate an array of 2^32 (or 4 times the address space, on a 32bit system). The compiler appears to be truncating 4096 * 4096 * 256 (Which is, off the top of my head, 0x10000) to a 32 bit value.

Depending on your platform, an unsigned long might also be 32bit, and also truncate.

I'd suggest you make sure you're compiling for a 64 bit platform (if that's what you're intending), or change the algorithm to either dynamically allocate memory (obviously no more than the address space), or wok with files on disk.

Rowland Shaw