Home  >  Article  >  Backend Development  >  Do all pointers in C have the same size? A deep dive into the complexities and nuances of pointer size in C .

Do all pointers in C have the same size? A deep dive into the complexities and nuances of pointer size in C .

Linda Hamilton
Linda HamiltonOriginal
2024-10-26 09:36:03935browse

Do all pointers in C   have the same size? A deep dive into the complexities and nuances of pointer size in C  .

Do All Pointers in C Have the Same Size?

The question of whether all pointers in C have the same size has been subject to debate. While some sources claim that pointers always have the same size, others suggest that different pointer types can have different sizes.

Guaranteed Sizes in the C Standard

The C standard explicitly guarantees that:

  • void has the same size as char.
  • T const, T volatile, and T const volatile have the same size as T.
  • Pointers to enum types with the same underlying type have the same size.

Practical Size Considerations

In practice, pointers to all class types and function types usually have the same size. This is because:

  • Compilers must determine the size of a pointer to an incomplete class type without knowing its implementation.
  • reinterpret_cast can be used to convert between different function pointer types without losing information.

Exception: Segmented Architectures

On segmented architectures, near and far pointers may have different sizes.

Reasons for Allowing Non-Uniform Size?

The C standard technically allows pointer types to have different sizes for pathological cases. However, in practical applications, such cases are rare and have no significant benefit.

Conclusion

While it's tempting to assume that all pointers in C have the same size, it's not guaranteed by the standard. However, in most practical scenarios, all pointers will have the same size, as outlined in the discussion above.

The above is the detailed content of Do all pointers in C have the same size? A deep dive into the complexities and nuances of pointer size in C .. 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