Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof Pointer differs for data type on same architecture

Tags:

c

pointers

sizeof

I have been going through some posts and noticed that pointers can be different sizes according to sizeof depending on the architecture the code is compiled for and running on. Seems reasonable enough to me (ie: 4-byte pointers on 32-bit architectures, 8-byte on 64-bit, makes total sense).

One thing that surprises me is that the size of a pointer can different based on the data type it points to. I would have assumed that, on a 32-bit architecture, all pointers would be 4-bytes in size, but it turns out that function pointers can be a different size (ie: larger than what I would have expected). Why is this, in the C programming language? I found an article that explains this for C++, and how the program may have to cope with virtual functions, but this doesn't seem to apply in pure C. Also, it seems the use of "far" and "near" pointers is no longer necessary, so I don't see those entering the equation.

So, in C, what justification, standard, or documentation describes why not all pointers are the same size on the same architecture?

Thanks!

like image 279
Cloud Avatar asked Jun 18 '14 22:06

Cloud


2 Answers

The C standard lays down the law on what's required:

  • All data pointers can be converted to void* and back without loss of information.
  • All struct-pointers have the same representation+alignment and can thus be converted to each other.
  • All union-pointers have the same representation+alignment and can thus be converted to each other.
  • All character pointers and void pointers have the same representation+alignment.
  • All pointers to qualified and unqualified compatible types shall have the same representation+alignment. (For example unsigned / signed versions of the same type are compatible)

  • All function pointers have the same representation+alignment and can be converted to any other function pointer type and back again.

Nothing more is required.
The committee arrived at these guarantees by examining all current implementations and machines and codifying as many guarantees as they could.

On architectures where pointers are naturally word pointers instead of character pointers, you get data pointers of different sizes.
On architectures with different size code / data spaces (many micro-processors), or where additional info is needed for properly invoking functions (like itanium, though they often hide that behind a data-pointer), you get code pointers of different size from data pointers.

like image 89
Deduplicator Avatar answered Sep 20 '22 22:09

Deduplicator


So, in C, what justification, standard, or documentation describes why not all pointers are the same size on the same architecture?

C11 : 6.2.5 p(28):

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.

6.3.2.3 Pointers p(8):

A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the pointed-to type, the behavior is undefined.

This clarifies that pointers to data and pointers to functions are not of the same size.

like image 26
haccks Avatar answered Sep 19 '22 22:09

haccks