void* will have the same representation and memory alignment as a pointer to char
Asked Answered
T

2

5

I am reading a book on pointers named "understanding and using c pointers"

When it comes to void * it says

It has two interesting properties:

  1. A pointer to void will have the same representation and memory alignment as a pointer to char.

What am confused about is isn't the memory of all the pointers same? They why instead of writing void* is same as normal pointer it explicitly mentioned char pointers? Will really appreciate any help

Tobias answered 29/3, 2017 at 11:18 Comment(3)
Maybe I am not reading it closely enough, but two properties are mentioned but only one listed?Grievance
If we stick to the hardware commonly used for desktop systems today, the answer is yes, all pointers are the same. But when you consider all the hardware out there (mainframes, embedded systems, mobile devices, special-purpose devices, etc), you can't safely make such statements. They are often the same, even usually, but it's no guarantee.Deangelo
@CareyGregory True about desktops. I suspect most CPUs these days are embedded ones where C is very popular.Padget
U
8

On most common architectures, pointer to any data type has the same representation, while pointer to function may differ. However, it's not a requirement, so it's possible to create valid C implementation, which uses different pointers for different data types. The reason behind this is that C standard tends to describe only crucial requirements, leaving a lot of freedom for possible implementations. Here is what standard says:

A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

If you're curious to see examples of systems with different sizes for different data types, this question mentions these wonderful examples

Unprincipled answered 29/3, 2017 at 18:29 Comment(1)
And for the OP's enjoyment, here's a link to an SO entry about a POSIX detail regarding function pointers and void*: https://mcmap.net/q/455226/-posix-restrictions-on-pointer-types-in-cOchs
O
-3

I think the point here is "memory alignment", not "memory size".

Yes, all pointers have same size of memory. But they may have different limitation for memory alignment.

For example, on some platforms, a "32-bit int" pointer must point to the address which should be times of 4 bytes. It cannot point to, e.g. 0x100001 or 0x100003.

But a "8-bit char" pointer can point to any address. So does a "void" pointer.

So it said that.

Overalls answered 29/3, 2017 at 21:14 Comment(1)
"all pointers have same size of memory." This is false, the standard does not guarantee it.Lesko

© 2022 - 2024 — McMap. All rights reserved.