Question

I thought the max value of a pointer was the highest possible value for the used pointer size, but apparently it isn't.

#include <iostream>
#include <limits>

int main() {
  std::cout << std::numeric_limits<char *>::is_bounded << std::endl;
  std::cout << (void *)std::numeric_limits<char *>::min() << std::endl;
  std::cout << (void *)std::numeric_limits<char *>::max() << std::endl;
}

I compile with g++ and get the following output:

0
0
0

The standard says min() and max() are meaningful if is_bounded is not false. It also says that is_bounded should be true if the set of values representable by the type is finite.

Are pointer not finite or why is is_bounded false and max() zero in my program?

Était-ce utile?

La solution

A pointer is no arithmetic type.

From 18.3.2.1 Class template numeric_limits

The numeric_limits class template provides a program with information about various properties of the implementation’s representation of the arithmetic types. Specializations shall be provided for each arithmetic type, both floating point and integer, including bool. The member is_specialized shall be true for all such specializations of numeric_limits. For all members declared static constexpr in the numeric_limits template, specializations shall define these values in such a way that they are usable as constant expressions. Non-arithmetic standard types, such as complex<T> (26.4.2), shall not have specializations.

Hence. your test applying a non arithmetic type leads to meaningless default values.

Autres conseils

The <limits> header does not implement a specific overload for std::numeric_limits<char *>. This means that we then use the default implementation.

The default implementation of std::numeric_limits<T>::min() and std::numeric_limits<T>::max() is to return T(), which would explain why you get 0.

You can see here the list of types that std::numeric_limits<T>::is_bounded() implements.

A pointer is a type in and of itself, and it is a type that stores the address of another variable. std::numeric_limits operates on types that deal with numbers. Pointers are not numbers, they are variables that store addresses to memory locations. Though the address itself maybe expressed numerically, that doesn't make pointers numbers.

min and max make no sense on pointers.

A 32-bit pointer can store an address of 32 bit size max. That's all. A 64 bit pointer has a max size of 64 bits, that means it can store an address of up to 64 bits in size. This has nothing to do with numeric min or max.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top