Home > Article > Backend Development > Are All Pointers the Same Size in C ?
Do All Pointers Have the Same Size in C ?
The question arises whether all pointers in C share a uniform size. While it's often assumed, the C standard provides definitive insights into this matter.
C Standard Specifications
The C standard explicitly guarantees:
Practical Considerations
In practice, pointers to class types generally maintain identical sizes. This is due to the requirement that the compiler must determine the size of T* without fully defining T. While compilers have some flexibility in pointer sizing, this is rarely exercised in real-world scenarios.
Additionally, function pointers, pointers to data members, and pointers to function members typically have the same size. This allows for seamless reinterpret_casting between these pointer types.
Exceptions
One notable exception occurs in segmented architectures, where near and far pointers may differ in size. This case defies the usual rules for pointer size uniformity.
Conclusion
The C standard clarifies that void* and cv-qualified pointers have a uniform size. While pointers to different types generally inherit the same size, practical considerations suggest that pointers to class types, function pointers, and pointers to data members all share a unified size. However, segmented architectures present an exception, with near and far pointers potentially having different sizes.
The above is the detailed content of Are All Pointers the Same Size in C ?. For more information, please follow other related articles on the PHP Chinese website!