What could be the largest value of sizeof (T)?

You might think of std::numeric_limits<size_t>::max() , but if there was an object that was huge, could it still offer a pointer to one end? Probably not. Does this mean that the largest value of sizeof(T) can give the value std::numeric_limits<size_t>::max()-1 ? Am I right or am I missing something?

+8
c ++ pointers sizeof memory size-t
source share
7 answers

Q: What is the largest value of sizeof (T)?

A: std::numeric_limits<size_t>::max()

Obviously, sizeof cannot return a value greater than std::numeric_limits<size_t>::max() , since it does not fit. The only question is, can it return ...::max() ?

Yes. Here is a valid program that does not violate the limitations of the C ++ 03 standard, which demonstrates the proof. In particular, this program does not violate any restrictions listed in clause 5.3.3 [expr.sizeof], as well as 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"; } 
+3
source share

If std::numeric_limits<ptrdiff_t>::max() > std::numeric_limits<size_t>::max() you can calculate the size of an object with the size std::numeric_limits<size_t>::max() by subtracting the pointer to it from pointer with one end to the end.

If sizeof(T*) > sizeof(size_t) , you can have enough different pointers to address each byte inside this object (for example, if you have an array from char) plus one for one end of the past.

So you can write an implementation where sizeof can return std::numeric_limits<size_t>::max() , and where you can get a pointer to one end of the end of a large object.

+3
source share

it is not exactly defined. but to stay within the safe limits of the standard, the maximum size of an object is std::numeric_limits<ptrdiff_t>::max()

because when you subtract two pointers you get ptrdiff_t

which is a signed integer type

greetings and hth.,

+2
source share

The requirement to be able to point outside the array has nothing to do with the size_t range. For an object x it is entirely possible that (&x)+1 is a valid pointer, even if the number of bytes separating the two pointers cannot be represented by size_t .

You can argue that this requirement implies an upper bound on the size of the object of the maximum range of pointers, minus the alignment of the object. However, I do not believe that the standard says that this type cannot be defined; it would be simply impossible to instantiate one and still remain compatible.

+1
source share

If it were a test, I would say (size_t) -1

0
source share

A sizeof() expression gives a value of type size_t . From standard C99 6.5.3.4:

The value of the result is determined by the implementation, and its type (unsigned integer type) is size_t, defined in stddef.h (and other headers).

Therefore, the maximum value sizeof () can get is SIZE_MAX.

0
source share

You may have a standard compatible compiler that allows you to use the sizes of objects that cause pointer arithmetic to overflow; however, the result is undefined. From the C ++ standard, 5.7 [expr.add]:

When two pointers to the elements of the same array object are subtracted, the result is the difference in the indices of the two arrays of elements. The result type is a signed implementation. integral type; this type must be of the same type as defined by std::ptrdiff_t in the <cstddef> (18.2) header. As with any other arithmetic overflow, if the result does not fit in the space provided, the behavior is undefined.

0
source share

All Articles