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

nnovich-OK
  • 2,900
  • 2
  • 13
  • 16
sara
  • 71
  • 6
  • Maybe I am not reading it closely enough, but two properties are mentioned but only one listed? – demongolem Mar 29 '17 at 19:53
  • 1
    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. – Carey Gregory Mar 29 '17 at 20:13
  • @CareyGregory True about desktops. I suspect most CPUs these days are embedded ones where C is very popular. – chux - Reinstate Monica Mar 29 '17 at 21:24

2 Answers2

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

Community
  • 1
  • 1
nnovich-OK
  • 2,900
  • 2
  • 13
  • 16
  • 1
    And for the OP's enjoyment, here's a link to an SO entry about a POSIX detail regarding function pointers and `void*`: http://stackoverflow.com/a/27707395/12711 – Michael Burr Mar 29 '17 at 21:29
-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.

Harrison
  • 313
  • 3
  • 15