Maximum size of literal values

Started by
4 comments, last by Barn Door 20 years, 9 months ago
Hi there, Assume that I have infinite memory, what is the maximum size of x in the following: void * ptr = new int[x] ? BD.
Advertisement
That might depend on your compiler settings; there may be a limit based on stack/heap size.

I have allocated megs and megs in single calls like that, but like they say, contiguous memory is a valuable asset.

www.cppnow.com
Assuming infinite memory? Well, the maximum value the variable type of x can hold, then.

[edited by - Zipster on July 6, 2003 3:36:40 PM]
There is no such thing in the standard as "infinite". Any type has a size. The type of "x" is, I believe, size_t, which is typically a 32-bit unsigned integer on common x86 CPUs. However, the CPU can''t address more than 32 bits'' worth of bytes in a linear fashion, and each int is 4 bytes, so that gives you 30 bits'' worth, or about 1 billion. However, the heap can''t grow that big, because most OS-es set aside half of linear memory for the kernel, leaving 29 bits of addressing or about 0.5 billion ints. Then you need space for your .exe and the .dlls it uses, so a more reasonable achievable maximum is something like 250 million ints.

On a 64-bit architecture, these numbers go up, of course :-)
Reserving that much memory takes AGES to do. I remember where at a point I tried to determine the max amount of int''s I could declare in a dynamic array. Took me about 2 minutes before it was done allocating it. I should try it again, maybe it was the setup of the computer.

Toolmaker


-Earth is 98% full. Please delete anybody you can.

Dear me!

void * ptr = new int[x]

Would this type mismatch even compile? I don''t have access to vc++ right now to check.

quote:
Assuming infinite memory? Well, the maximum value the variable type of x can hold, then.


I didn''t mean for x to be a C++ variable. I meant it to be a literal value like this... int[60000]

What I''m trying to get at is does c++ allow literal values in code which are too big to fit into any basic type?

This topic is closed to new replies.

Advertisement