At first one might think std::numeric_limits<size_t>::max()
, but if there was an object that huge, could it still offer a one-past-the-end pointer? I guess not. Does that imply the largest value sizeof(T)
could yield is std::numeric_limits<size_t>::max()-1
? Am I right, or am I missing something?
The value of this constant is 2,147,483,647; that is, hexadecimal 0x7FFFFFFF.
So, the sizeof(int) simply implies the value of size of an integer. Whether it is a 32-bit Machine or 64-bit machine, sizeof(int) will always return a value 4 as the size of an integer.
The sizeof operator returns the number of bytes in a variable type, or the number of bytes occupied by an array.
Q: What is the largest value sizeof(T) can yield?
A: std::numeric_limits<size_t>::max()
Clearly, sizeof cannot return a value larger than std::numeric_limits<size_t>::max()
, since it wouldn't fit. The only question is, can it return ...::max()
?
Yes. Here is a valid program, that violates no constraints of the C++03 standard, which demonstrates a proof-by-example. In particular, this program does not violate any constraint listed in §5.3.3 [expr.sizeof], nor in §8.3.4 [dcl.array]:
#include <limits>
#include <iostream>
int main () {
typedef char T[std::numeric_limits<size_t>::max()];
std::cout << sizeof(T)<<"\n";
}
If std::numeric_limits<ptrdiff_t>::max() > std::numeric_limits<size_t>::max()
you can compute the size of an object of size std::numeric_limits<size_t>::max()
by subtracting a pointer to it from a one-past-the-end pointer.
If sizeof(T*) > sizeof(size_t)
you can have enough distinct pointers to address each and every single byte inside that object (in case you have an array of char, for example) plus one for one-past-the-end.
So, it's possible to write an implementation where sizeof
can return std::numeric_limits<size_t>::max()
, and where you can get pointer to one-past-the-end of an object that large.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With