Home > Backend Development > C++ > Do Pointers Always Have the Same Size in C ?

Do Pointers Always Have the Same Size in C ?

Mary-Kate Olsen
Release: 2024-10-28 22:42:02
Original
658 people have browsed it

 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:

  • void has the same size as char
  • cv-qualified pointers (e.g., T const, T volatile, T const volatile) have the same size as T
  • Pointers to enum types with the same underlying type have the same size

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!

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