Are anonymous unions standard C?
Are anonymous unions standard C?
The rules for unions and structs are the same, so yes.
>Are anonymous unions standard C?
No.
My best code is written with the delete key.
Right, so one person says yes, and the other says no.
I'd be inclined to believe Prelude is right - but I have a feeling it may be "confusion" about the concept of anonymous struct/union's here:
Is definitely allowed in the standard [I just looked it up]. Which is what I think tabstop meant.Code:struct { // stuff goes here } blah;
However, another usage of "anynymous struct/union" is this one:
This isn't allowed in the standard, but several mainstream compilers (gcc, MS) support this.Code:struct something { int x; int y; }; struct someother { struct something; int z; }; ... struct someother a; a.x = 1; a.y = 2; a.z = 3;
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
>I have a feeling it may be "confusion" about the concept of anonymous struct/union's
Quite likely. I was referring to the actual anonymous union extension in your second example. The first example I would call unnamed rather than anonymous. I'd also accept "anonymous" as a variant term for a compound literal in C99:
But that's a bit of a stretch.Code:foo ( (struct bar) { .a = 0, .b = 1 } );![]()
My best code is written with the delete key.
Here's another fun one, the infamous struct hack:
Sadly, this abomination has been standardized in C99:Code:#include <stdio.h> #include <stdlib.h> struct foo { int a; int b[1]; }; int main ( void ) { struct foo *p = malloc ( sizeof *p + 10 * sizeof ( int ) ); int i; p->a = 10; for ( i = 0; i < p->a; i++ ) p->b[i] = i * i; for ( i = 0; i < p->a; i++ ) printf ( "%d\n", p->b[i] ); free ( p ); return 0; }
Code:#include <stdio.h> #include <stdlib.h> struct foo { int a; int b[]; }; int main ( void ) { struct foo *p = malloc ( sizeof *p + 10 * sizeof ( int ) ); int i; p->a = 10; for ( i = 0; i < p->a; i++ ) p->b[i] = i * i; for ( i = 0; i < p->a; i++ ) printf ( "%d\n", p->b[i] ); free ( p ); return 0; }
My best code is written with the delete key.
In your first example, should that be:Code:struct foo *p = malloc ( sizeof *p + 10 * sizeof ( int ) );
??Code:struct foo *p = malloc ( sizeof *p + 9 * sizeof ( int ) );
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
And does "int b[]" contribute anything to the "sizeof *p" under C99?
gg
The standard says that sizeof should return the offset of the flexible array member, so I'm pretty sure the answer is no. The example they give (involving doubles with sizeof(double)==8) malloc's sizeof() plus 64 to get an 8-element array.
>In your first example, should that be:
Not if you want to be absolutely safe across all implementation. I've seen a case where the implementation assumes a trailing array of size 1 is the struct hack (stupid, I know), and doesn't include it in the result of sizeof.
>And does "int b[]" contribute anything to the "sizeof *p" under C99?
No, "sizeof ( struct foo )" is equivalent to "offsetof ( struct foo, b )".
My best code is written with the delete key.
Anonymous unions are standard C++, but not standard C.
Basically, you declare a union with no tag and no instances, and you can access all members of the union by name with no prefix. In addition, the members behave like ordinary union members in that they occupy the same memory.Code:#include <iostream> int main() { union { int n; char c; }; std::cin >> c; std::cout << n; return 0; }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.