Take the 2-minute tour ×

I am trying right now to declare a large character array. I am using the character array as a bitmap (as in a map of booleans, not the image file type). The following code generates a compilation error.

//This is code before main. I want these as globals.
unsigned const long bitmap_size = (ULONG_MAX/(sizeof(char)));
char bitmap[bitmap_size];

The error is overflow in array dimension. I recognize that I'm trying to have my process consume a lot of data and that there might be some limit in place that prevents me from doing so. I am curious as to whether I am making a syntax error or if I need to request more resources from the kernel. Also, I have no interest in creating a bitmap with some class. Thank you for your time.

EDIT ULONG_MAX is very much dependent upon the machine that you are using. On the particular machine I was compiling my code on it was well over 4.2 billion. All in all, I wouldn't to use that constant like a constant, at least for the purpose of memory allocation.

share|improve this question
Just FYI, sizeof(char) is always 1, so the division is pointless. –  Luchian Grigore Feb 23 '12 at 22:33
you know that you are trying to allocate 4 gigabytes of memory on 32 bit machine? –  fazo Feb 23 '12 at 22:33
consequent 4 gigabytes of memory –  triclosan Feb 23 '12 at 22:35
And it's all static memory. Why would you do this? –  brendanw Feb 23 '12 at 22:36
@brendanw The array will be needed throughout the life of the program. I know the size of the bitmap I need. Is there something inherently wrong static allocation. Also, are you able to malloc outside of function? –  order Feb 23 '12 at 22:43
show 2 more comments

3 Answers

up vote 4 down vote accepted

ULONG_MAX/sizeof(char) is the same as ULONG_MAX, which is a very large number. So large, in fact, that you don't have room for it even in virtual memory (because ULONG_MAX is probably the number of bytes in your entire virtual memory).

You definitely need to rethink what you are trying to do.

share|improve this answer
Thanks. I was meaning to divide ULONG_MAX by the number of bits in a char rather than the number of bytes. The compiler no longer complained after this. –  order Feb 23 '12 at 22:56
add comment

As I understand it, the maximum size of an array in c++ is the largest integer the platform supports. It is likely that your long-type bitmap_size constant exceeds that limit.

share|improve this answer
add comment

It's impossible to declare an array that large on most systems -- on a 32-bit system, that array is 4 GB, which doesn't fit into the available address space, and on most 64-bit systems, it's 16 exabytes (16 million terabytes), which doesn't fit into the available address space there either (and, incidentally, may be more memory than exists on the entire planet).

Use malloc() to allocate large amounts of memory. But be realistic. :)

share|improve this answer
add comment

Your Answer


