Anonymous nested structs are also quite useful for creating struct fields with explicit offsets: #include #include #define YDUMMY(suffix, size) char dummy##suffix[size] #define XDUMMY(suffix, size) YDUMMY(suffix, size) #define PAD(size) XDUMMY(__COUNTER__, size) struct ExplicitLayoutStruct { union { struct __attribute__((packed)) { PAD(3); uint32_t foo; }; struct __attribute__((packed)) { PAD(5); uint16_t bar; }; str…
Anytime macros are used for metaprogramming, it's time to reach for a more powerful language.
Learning that you can use unions in C for grouping things into namespaces
51–60 of 150 posts
Re: Learning that you can use unions in C for grouping things into namespaces
#52Earlier quoted context omitted.
> C++ incompatibly requires a name for all struct and class declarations You're right about "enum class", but anonymous classes and structs are perfectly valid in C++: https://godbolt.org/z/7MbcqhnoK
Try struct S { struct { int x; }; }; under -pedantic and you'll get warning: ISO C++ prohibits anonymous structs [-Wpedantic]
Re: Learning that you can use unions in C for grouping things into namespaces
#53Don't actually do this.
The Linux kernel is using this for bounds checking. https://news.ycombinator.com/item?id=28015263
Re: Learning that you can use unions in C for grouping things into namespaces
#54Earlier quoted context omitted.
I believe you are mistaken. The C11 standard, section 6.5.2.3 "Structure and union members" pgf 6, says "One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declar…
No: from https://en.cppreference.com/w/cpp/language/union . The union is only as big as necessary to hold its largest data member. The other data members are allocated in the same bytes as part of that largest member. The details of that allocation are implementation-defined but all non-static data members will have the same address (since C++14). It's undefined behavior to read from the member of the union that wasn…
Re: Learning that you can use unions in C for grouping things into namespaces
#55Earlier quoted context omitted.
I believe you are mistaken. The C11 standard, section 6.5.2.3 "Structure and union members" pgf 6, says "One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declar…
No: from https://en.cppreference.com/w/cpp/language/union . The union is only as big as necessary to hold its largest data member. The other data members are allocated in the same bytes as part of that largest member. The details of that allocation are implementation-defined but all non-static data members will have the same address (since C++14). It's undefined behavior to read from the member of the union that wasn…
Re: Learning that you can use unions in C for grouping things into namespaces
#56Earlier quoted context omitted.
Try struct S { struct { int x; }; }; under -pedantic and you'll get warning: ISO C++ prohibits anonymous structs [-Wpedantic]
Pedantic is for the older C++ standard, its not pedantic for the latter e.g c++11, I think this changed.
Re: Learning that you can use unions in C for grouping things into namespaces
#57Bleurgh. I have a deep soft spot for C, and I'm known to get twisted pleasure from using obscure language features in new ways to annoy people, but this is a level of abuse that even I can't get behind. If you need namespacing, use C++. As much as I love C, it's terrible for large projects.
Re: Learning that you can use unions in C for grouping things into namespaces
#58Earlier quoted context omitted.
There are two kinds of undefined behaviour being invoked in using this. Its a horrible idea and a horrible code smell, get rid of it if you ever see something like this.
I don't see any undefined behavior here. As I mentioned below, gcc explicitly documents type punning via unions as being well defined. But yes, this is compiler specific and is not guaranteed to work elsewhere.
The correct way to access packed structs is through memcpy, just like you'd access any other potentially unaligned object.
Re: Learning that you can use unions in C for grouping things into namespaces
#59Earlier quoted context omitted.
Try struct S { struct { int x; }; }; under -pedantic and you'll get warning: ISO C++ prohibits anonymous structs [-Wpedantic]
Pedantic is for the older C++ standard, its not pedantic for the latter e.g c++11, I think this changed.
Re: Learning that you can use unions in C for grouping things into namespaces
#60Earlier quoted context omitted.
I don't see any undefined behavior here. As I mentioned below, gcc explicitly documents type punning via unions as being well defined. But yes, this is compiler specific and is not guaranteed to work elsewhere.
There is absolutely undefined behaviour there. Undefined behaviour is defined not as nasal daemons but as: The compiler implementer does not guarantee that this behaviour will be hardware, circumstance, compiler version, or os consistent, nor that we will warn if we change this. Packed is technically not a undefined behaviour, but it is certainly a trap. Especially because the compiler macros leads people to make def…