0

I recently came across this comment by @Paul Ogilvie:

"You say "To define a pointer to a structure you only need to know the structure tag". In my experience that is unnecessary. Just declare a pointer_to_some_type and the compiler will reserve space for a pointer and does type checking on assignment. Only once you want dereference the pointer and access its members, must the type fully be known."

I tried it out with:

struct foo {
    int a;
    struct bar* x;
};

struct bar {
    int b;
};

and indeed neither GCC nor Clang doesn't throw any diagnostic, as opposed to attempt to defining an object of the respective struct without forward-declaration.

Evidence

But that makes me wonder: Is this standard-compliant?

Citations from the standard are highly appreciated.

  • 3
    The quoted text is incorrect with regard to what the C standard defines (not with regard to what the author’s experiences may have been). In C, pointers of different kinds may generally have different sizes and representations. There are some requirements, such as that pointers to structures have the same size and representation, and that pointers to `char` and `void` have the same size and representation. So you can define a member to be `struct Anything *` and later use it as a `struct SomethingElse *`, provided the right conversions are used. But using `MyType *`, is not required to work. – Eric Postpischil Jul 10 '20 at 19:10
  • @LanguageLawyer Very well found. I couldn't. Anyway, I'm still struggling whether I accept it as duplicate or not. One of the main aspects of my question is the standard-proof if the sizes of pointers to any structure are the same on the same implementation so that there is no problem in detecting the exact size of the "container" structure, which dbush also already covered very well. The answers to the proposed duplicate question covers "only" if the declaration itself is valid, but none of them gives standard-proof about the pointers being of exact same size. – RobertS supports Monica Cellio Jul 11 '20 at 08:13
  • They only say it but give no concrete proof. I also ask the answers myself about that. I edited my question title to be a bit more specific. Its a hair-splitting case. – RobertS supports Monica Cellio Jul 11 '20 at 08:13
  • If you want to know if it is legal to reference an undeclared struct in pointer declaration, then ask about this. If you want to know if all pointers to structs are of the same size, ask about this. Is it necessary to mix these 2 questions? Looks artificial. – Language Lawyer Jul 11 '20 at 08:18
  • @LanguageLawyer It's kind of hard since the two closely belong to each other. What about the reformed title now? – RobertS supports Monica Cellio Jul 11 '20 at 08:24
  • I also take suggestions for other titles. – RobertS supports Monica Cellio Jul 11 '20 at 08:30
  • 1
    _What about the reformed title now?_ Looks like you're just trying to make a dup artificially not be a dup. – Language Lawyer Jul 11 '20 at 08:30
  • @LanguageLawyer Problem is I have 3 options: 1. I mark this as dupe and edit the title to ask only if this the declaration of an incomplete type in a structure is valid, but the given answer would be not appropriate any longer. 2. I rephrase this question so that it matches to the answer, but it loses focus to the thing I've asked for. 3. I close this question as dupe and let the question cover both things so that the answer is still appropriate. - What would you do? – RobertS supports Monica Cellio Jul 11 '20 at 08:45
  • @LanguageLawyer I accepted my question being a duplicate finally. Thank you for finding the other question. – RobertS supports Monica Cellio Jul 11 '20 at 08:58
  • @LanguageLawyer I splitted both questions as it indeed should be. The other question about if the sizes of the pointers are is now [here](https://stackoverflow.com/q/62847600/12139179) - I decided to give it a bigger frame on its own. Thank you for your input. – RobertS supports Monica Cellio Jul 11 '20 at 10:11

1 Answers1

2

If the representations are the same, that implies that the sizes are the same.

Section 6.2.6.1p2 of the C standard states:

Values stored in non-bit-field objects of any other object type [ed. not unsigned char] consist of n×CHAR_BIT bits, where n is the size of an object of that type, in bytes. The value may be copied into an object of type unsigned char [n] (e.g., by memcpy); the resulting set of bytes is called the object representation of the value. Values stored in bit-fields consist of m bits, where m is the size specified for the bit-field. The object representation is the set of m bits the bit-field comprises in the addressable storage unit holding it. Tw o values (other than NaNs) with the same object representation compare equal, but values that compare equal may have different object representations.

As an example, an int and a short under gcc both use two's complement but they don't have the same object representation because one is 4 bytes and the other is 2 bytes.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • I didn't ask the question very well and included two questions in one. My bad, I'm so sorry. I accepted being a dupe and want to ask you if you can implement this answer to [my other question specific question](https://stackoverflow.com/q/62847600/12139179) so that everything fits well and people can find your answer better as well, too. Unless it doesn't get closed soon. – RobertS supports Monica Cellio Jul 11 '20 at 09:49