Live data from Hacker News

Learning that you can use unions in C for grouping things into namespaces

utcc.utoronto.ca

51–60 of 150 posts

Re: Learning that you can use unions in C for grouping things into namespaces

#51

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.

I want to be clear about your meaning, because I don’t know if I’m reading your comment correctly. Are you referring explicitly to syntax based, preprocessor macros? Or does your comment extend to other metaprogramming techniques? I am inclined to think you mean the first considering the amount of emphasis on generic programming in D? Just curious.

Re: Learning that you can use unions in C for grouping things into namespaces

#52
post #24

Earlier 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]

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

#53
post #16

Don't actually do this.

The Linux kernel is using this for bounds checking. https://news.ycombinator.com/item?id=28015263

Like the parent poster, when I read the article I assumed that there was no conceivable reason to ever use this feature in a real C program. Let me just say that I'm pleasantly surprised to be proven wrong!

Re: Learning that you can use unions in C for grouping things into namespaces

#54
post #48
post #46

Earlier 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…

Your response to GP is based on the C++ reference and his explicitly is based on the C standard. Your assertion that ‘ [t]he details of that allocation are implementation-defined but all non-static data members will have the same address (since C++14)’ seems to directly conflict with the C11 standard. Also, your closing comment about std::variant is clearly only applicable to C++. I am just curious why you are using C++ when the article and GP are specifically addressing C?

Re: Learning that you can use unions in C for grouping things into namespaces

#55
post #48
post #46

Earlier 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…

The post is about C, not C++. My comment stands, as the original post has two structs in a union, and they start the same way, so it’s exactly the case covered in the C11 Standard.

Re: Learning that you can use unions in C for grouping things into namespaces

#56
post #52

Earlier 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.

Well that blows my mind, I never realized pedantic ignores the language setting. Is this the only case where it does that?

Re: Learning that you can use unions in C for grouping things into namespaces

#57

Bleurgh. 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.

People will make a mess of a large project regardless of the language.

Re: Learning that you can use unions in C for grouping things into namespaces

#58
post #31

Earlier 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.

Accessing packed struct members works fine on x86, but will blow up at runtime or do weird things on platforms which don't support unaligned loads or stores.

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

#59
post #52

Earlier 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.

No, pedantic is for disabling compiler extensions. You still need to explicitly specify a standard.

Re: Learning that you can use unions in C for grouping things into namespaces

#60
post #50

Earlier 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…

You don't get to decide what UB means. It really does mean nasal demons are a possibility: all bets are off when you run that executable. Use of the term "undefined behaviour" to mean something else may be on the increase, unfortunately (https://mars.nasa.gov/technology/helicopter/status/298/what-...), but if we're talking about C, it's meaning is fixed.
Post reply on HN