Home >Backend Development >C++ >Do Pointers Always Have the Same Size in C ?
Do All Pointers Have the Same Size in C ?
This question has been a subject of debate, with some sources claiming that all pointers have the same size while others argue that pointers can vary in size.
According to the C standard, it is explicitly guaranteed that:
While the standard does not explicitly state that all pointers have the same size, it is generally true in practice that pointers to all class types, function pointers, data member pointers, and function member pointers have the same size.
However, this assumption is not 100% guaranteed, as compilers are technically allowed to play tricks. For example, they could create longer pointers for classes that start with a particular prefix or if they decide to implement garbage collection for those classes.
In practical scenarios, it is highly unlikely that compilers will utilize this freedom. Consequently, developers can generally assume that pointers to different class types and other types mentioned above will have the same size. To provide further assurance, a static_assert statement can be implemented to flag potential violations.
However, in segmented architectures with near and far pointers, it is possible that these types of pointers may not have the same size. This serves as an exception to the usual rule of pointers having uniform sizes.
The above is the detailed content of Do Pointers Always Have the Same Size in C ?. For more information, please follow other related articles on the PHP Chinese website!