Why can't this statement be compiled?

Jul 12, 2011 at 11:03am

int Century[100][365][24][60][60];

Despite my memory being larger than 2 GB?
Jul 12, 2011 at 11:05am

It will fail because that is an attempt to create an object on the stack, and your stack is not 2 GB in size. We call this "Stack overflow"; http://en.wikipedia.org/wiki/Stack_overflow

Last edited on Jul 12, 2011 at 11:07am
Jul 12, 2011 at 11:08am
Whats a stack?
Jul 12, 2011 at 11:09am
It's a section of memory. Follow that link, follow the links in that link. Read about memory.
Jul 12, 2011 at 11:10am
But I get the error while compiling not while running!
Jul 12, 2011 at 11:14am
What is the error then?
Last edited on Jul 12, 2011 at 11:15am
Jul 12, 2011 at 11:15am
Your compiler has limits also. It has to work out in advance where things will go in memory and all that sort of thing. Clearly your compiler is unable to deal with an array of this size.

If you must have an array of this size, I suggest that you firstly think of a better way to do it, and secondly don't use arrays but start using C++ containers.
Jul 12, 2011 at 11:18am
It says: error C2148: total size of array must not exceed 0x7fffffff bytes

What does this number 0x7fffffff physically mean?(I know its a hex number and its value)

I use VS 2010 Pro
Jul 12, 2011 at 11:26am
That is 2,147,483,647 bytes, which is the largest value a 32-bit signed long can represent. It is just one under 2^31 (or one under (2^32)/2). Recall a byte is generally 8 bits and is the smallest that C++ can deal with, char is a byte, short is two bytes, int is generally but not always 4 bytes, long is 4 bytes, long long is 8 bytes, float is 4 bytes, double is 8 bytes, and sometimes long double is better than double but for some compilers they are the same.

If you array exceeds the maximum number of bytes then you're out of luck.
Last edited on Jul 12, 2011 at 11:27am
Jul 12, 2011 at 11:31am
Its (2^31)-1.

That's the largest signed integer value your system can handle, and it needs those integers to handle memory. You're coming up against the limits of your system.


Jul 12, 2011 at 12:11pm
You're probably trying to compile this in 32-bit mode, so declaring an array this large can't possibly be done.
It should work when compiling for a 64-bit target.
Jul 12, 2011 at 4:20pm
@SameerThigale if you don't even know what a stack is, I suggest reading some c++ books to get down the basics about c++ programming.
Jul 16, 2011 at 2:12pm
Just for the problem: do you intend to count something within a century, each second separately?
Jul 17, 2011 at 3:46am
If you do that [365] is wrong.
Jul 17, 2011 at 6:52am
@morus: NO

@buffbill: What?
Jul 17, 2011 at 6:55am
I think buffbill is talking about leap years, which means you wouldn't have exactly 365 days per year. What is this for anyway? It seems really useless.
Jul 17, 2011 at 7:27am
@firedraco: Yeah!
Jul 17, 2011 at 3:06pm
Works on my 64 bit system. I even assigned values into it at runtime.
Topic archived. No new replies allowed.