Re: max size of ptr array



"Alan Carre" <alan@xxxxxxxxxxxxxxxxx> wrote in message
news:uph0TN43IHA.5012@xxxxxxxxxxxxxxxxxxxxxxx
"Daniel" <newsonly@xxxxxxxxxxxx> wrote in message
news:O3RYAcY3IHA.4988@xxxxxxxxxxxxxxxxxxxxxxx
If I declare an array of pointers:
ptr = new int[1000];

What is the largest number that I can put inside of the [] other than
1000?

Curiosity: Obviously you're just starting out with this stuff, so some of
the words and ideas expressed here are obviously unfamiliar to you. People
talking about size_t and "implementations" and "heap fragmentation" and so
on... which is not very helpful for you I presume.

I'm getting the feeling that your question is more likely a question about
what you're allowed to "write" in the brackets such that the compiler
doesn't start complaining. Like if you said:

int* ptr = new int[100000000000000000000000000000000000000];

Well, you'll get a compiler error. Right?

Am I correct in that assumption? That that what you're really asking?
"what is the largest 'legal' number we can write in the brackets?" (where
'legal' just means the compiler doesn't complain).

If so, there is no correct answer to your question because it depends on
this definition of 'legal', or 'the compiler'. If that's not what you're
asking about you need not read any further.

--------------------------------------------------
Why is there no 'correct' answer to the question?

Let's say, for argument's sake, we're dealing with "normal, year 2008,
latest microsoft Win32 compiler" conditions. In that case then the largest
number you are allowed type in the brackets is exactly 536870911. Just try
it!

This compiles:
=========
int main() {
int* ptr = new int[536870911];
}

This does not:
=========
int main() {
int* ptr = new int[536870912]; <-- compile error C2148
}

-----------------------

The actual error, i mean the full text is:
error C2148: total size of array must not exceed 0x7fffffff bytes

Which must seem like complete gibberish because you probably aren't
familiar with this "0x7fffffff" business. If you are, then my apologies.
If you aren't, then I'll just say, without explaining, that that is what
is called "hexadecimal" notation. It's just another way to write numbers.
And there's an easy way to convert numbers written this way into "normal"
numbers. You can use windows "calc" to do it. Choose "scientific" and
click on "Hex" write down 7fffffff (without the 0x part) and then click on
"Dec" (for decimal) and it will convert it for you!

Now there's a techical reason for this 0x7fffffff limitation but very
little point in going into it at this stage. The main thing I wanted to
point out was this: Read the error line again. Notice it says *bytes* not
*ints*.

It so happens that in the "normal 2008 microsoft Win32 world" it takes 4
"bytes" to make up an "int". So if we go to "calc" switch to Hex, type in
7fffffff following the steps above, we discover that:

0x7fffffff (Hex) = 2147483647 (Decimal)

And since an int requires 4 "bytes" plus the fact that your array is
composed of "ints", we decuce that the maximum value you can type in
brackets must be 2147483647 / 4 = 536870911. And that's all there is to
it!

---------------------

Lastly, what this all adds up to is essentially what I said before. "There
is no correct answer to your question".

If you wanted an array of "bytes" then the maximum number you're allowed
to type in the brackets is 2147483647. If it's an array of its, you're not
allowed to type a number bigger than 536870911. If it's an array of
"shorts" then it's 1073741823 (why? because a short only requires 2
"bytes", not 4).

If the current year was 1985, then the maximum number you'd be allowed to
type in *for int* would be 16383. For bytes it would be 32767. Why?
Because in 1985 an int required only 2 bytes, but the maximum allowed
array size (in bytes) was a piddly 7fff = 32767. So, as you can see, all
of this depends on the "environment" you're working in and the size of the
individual objects in the array, and *neither* of those is a fixed
quantity.

--------------------

But I guess I could've stuck to the simple answer: 536870911 and left it
there ;)

I hope that helps,
- Alan Carre

Great answer Alan.
Ron Francis.


.



Relevant Pages

  • Re: Error handling library
    ... which lets the compiler catch out-of-range usage. ... and assuming that a higher int means "more dangerous error" ... with a comment warning that one is used as an index into the array ... languages while running (an array of languages, ...
    (comp.lang.c)
  • Re: Maximum char array size?
    ... versus declaring an array at compile time (other than the need ... I did not get anywhere before I declared the new type ARRAY2. ... and DMC (Digital Mars Compiler) on WinXp. ... void PrintArray(char *name, int *array, int ncol, int nrow) ...
    (comp.lang.c)
  • Re: On VLAs and incomplete types
    ... I don't understand how a variable is handled by the compiler in ... declaring a VLA. ... an array must be declared with an ... int x; ...
    (comp.lang.c)
  • Re: max size of ptr array
    ... you'll get a compiler error. ... number you are allowed type in the brackets is exactly 536870911. ... int main{ ... And since an int requires 4 "bytes" plus the fact that your array is ...
    (microsoft.public.vc.language)
  • random number code
    ... Will it really compile exactly the same as mingw compiler used ... // initialization of static private members ... MSBs of the seed affect only MSBs of the array ... // constructor with 32 bit int as seed ...
    (comp.lang.cpp)

Loading