Home > Backend Development > C++ > Are All Pointers the Same Size in C ?

Are All Pointers the Same Size in C ?

Mary-Kate Olsen
Release: 2024-10-31 00:14:30
Original
814 people have browsed it

 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:

  • void and char are of equal size (section [basic.compound]/5).
  • Pointers to different cv-qualified versions of the same type (T const, T volatile, and T const volatile*) share the same size ([basic.compound]/3).
  • Pointers to enum types with identical underlying types have the same size (section [dcl.enum]/9).

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template