Home  >  Article  >  Backend Development  >  Do All Pointers in C Have the Same Size? The Surprising Answer.

Do All Pointers in C Have the Same Size? The Surprising Answer.

Patricia Arquette
Patricia ArquetteOriginal
2024-10-26 19:13:02927browse

 Do All Pointers in C   Have the Same Size?  The Surprising Answer.

Do All Pointers Have the Same Size in C ?

In the world of C , pointers play a crucial role in managing memory and accessing data. But do they all adhere to the same size constraints? Surprisingly, the answer is both yes and no.

The C Standard's Stance

While it's tempting to assume all pointers share the same size due to their inherent nature as memory addresses, the C standard explicitly delineates certain cases where this assumption holds true:


  1. void has the same size as char ([basic.compound]/5)
  2. T const, T volatile, and T const volatile have the same size as T. This stems from the fact that cv-qualified versions of the same type exhibit layout compatibility, and pointers to layout-compatible types possess an equivalent value representation ([basic.compound]/3).
  3. Similarly, pointers to enum types with the same underlying type share the same size ([dcl.enum]/9).

The Practical Reality

In practical terms, pointers to class types typically adhere to the same size constraint. The reason lies in the standard requirement that even when the class type is incomplete, the size of its pointer type can still be obtained. This imposes a restriction on compilers, forcing them to determine the size of the pointer type without complete knowledge of the class's internal structure.

While compilers retain the flexibility to introduce size disparities, such as differentiating pointers based on class prefixes, this practice is uncommon in real-world scenarios.

In most cases, the following size equalities also hold true in practice:


  • Pointers to function types have identical sizes.
  • Pointers to data members have the same size.
  • Pointers to function members possess the same size.

These equalities arise from the ability to reinterpret cast between certain pointer types without information loss. While compilers could technically introduce padding variations, no practical justification exists for doing so.

Exceptions to the Rule

However, in segmented architectures with "near" and "far" pointers, you may encounter size differences. This divergence from the general rules is a notable exception.

In conclusion, while the C standard allows for the possibility of pointer size variations, in practical applications, most pointers conform to the same size constraints. This consistency simplifies memory management and enhances code readability.

The above is the detailed content of Do All Pointers in C Have the Same Size? The Surprising Answer.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to Define a Virtual `Next article:How to Define a Virtual `