2

N2346/6.2.5p28:

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.

So there must be an example of 2 function pointers having different representation and/or alignment requirements. I'm only familiar with x86 uarchs and there is no such an example on x86 architectures.

Could you suggest an architecture and example exposing such pointer behavior?

Some Name
  • 8,555
  • 5
  • 27
  • 77
  • 2
    "need not" does not mean "there is". – the busybee Aug 20 '21 at 06:44
  • @thebusybee Anyway there must be a reason for that... no? – Some Name Aug 20 '21 at 06:45
  • 2
    far and near function pointers. The reason is: write the safe portable code even if such a architecture was not invented yet. But it can be in the future. – 0___________ Aug 20 '21 at 06:45
  • @0___________ Can't I use `far` and `near` for structures and unions, too? – the busybee Aug 20 '21 at 06:48
  • Well, the C standard is full of such details. I would not expect a (good) reason for any decision. -- It could be the other way around: The standard did _not_ want to require same representation and alignment for pointers at all. But something might have happened to enforce this for structures and unions. So we could ask "Why are pointers to structures and unions required to ...?" – the busybee Aug 20 '21 at 06:51
  • @thebusybee _Why are pointers to structures and unions required to ...?_ that's a good question actually. Do you have any ideas about it? – Some Name Aug 20 '21 at 07:09
  • Not at all, as several compilers I know allow structure pointers of different kinds. However, most probably these are a non-compliant extensions. -- As in all similar questions concerning the standard: Is there no way to ask the committee? They should know. – the busybee Aug 20 '21 at 07:12
  • With regards to structs and unions - I asked about this here https://stackoverflow.com/questions/68718097/why-must-all-pointers-to-structs-be-of-the-same-size - the answers/comments here also refer to near/far/huge pointers, and how these aren't addressed by the standard but require C extensions – Daniel Kleinstein Aug 20 '21 at 07:29
  • 3
    Separated address spaces - different pointer formats for code and data – 0___________ Aug 20 '21 at 07:46
  • n1570.html#6.3.2.3p8 guarantees that any two function pointers of different type can be **converted** between one and another. Though it does not force them to have the same binary representation – tstanisl Aug 20 '21 at 08:14
  • Cray T90 perhaps – Support Ukraine Aug 20 '21 at 08:14
  • BTW: Why do you ask specific about function pointers? The quoted part is not specific about function pointers – Support Ukraine Aug 20 '21 at 08:33
  • 3
    Pointers to structure types must have the same representation as each other because one translation unit can have a pointer to an unknown structure type, `struct foo *p;`, and another translation unit can have a pointer to a known structure type, `struct foo { int member; … } *p`, and we want these translation units to be able to pass a pointer to `p` and be able to use it as a pointer to the type they have for the structure. So `struct foo *` must have the same representation as a pointer to any type of structure, so they must all be the same. Same for unions. – Eric Postpischil Aug 20 '21 at 11:18
  • 2
    That explains why the standard says all pointers to structures must have the same representation as each other and why all pointers to unions must have the same representation as each other. The same need does not exist for pointers to functions, so the standard simply does not bother to say that all pointers to functions must have the same representation as each other. It does not mean counterexamples must exist. – Eric Postpischil Aug 20 '21 at 11:19
  • @EricPostpischil Sounds pretty reasonable. Would you post it as an answer I think it might be very useful... ? – Some Name Aug 20 '21 at 13:24
  • 1
    @SomeName: I have been considering it, but it needs a little more development. I can make the case that all structure pointers have to be the same size so that one can make them members of other structures and pass them around, but it is harder to make the case that the representation has to be completely the same, because that requires a good use case where two different modules want to use the same pointer with different types. Or maybe it is interaction with the rules for matching function arguments to function parameters that makes having the same representation convenient. – Eric Postpischil Aug 23 '21 at 18:21

0 Answers0