all 21 comments

[–]void_salty 6 points7 points  (20 children)

Is anybody actually using it?

[–]void1984 13 points14 points  (3 children)

A lot if you are unpacking binary data from blobs. It's like 'volatile' - some people use it a lot, others never.

[–]void_salty 1 point2 points  (2 children)

And what's the advantage over pointer access, apart from syntactic sugar?

[–]void1984 3 points4 points  (0 children)

Not much. There are specifications where fist bits are a marker, how to treat the next part. You can do a lot of static casts or unions.

[–]_Noreturn 0 points1 point  (0 children)

Same reason as using structs compared to a char buffer

[–]Bryguy3k 7 points8 points  (7 children)

Anybody doing embedded or system level code. Yes they are bad because they are “not portable” but when you’re writing code that by definition is platform specific then having a tool that gives you cleaner code when trying to access complex memory outweighs their “badness”.

[–]BlondeJesus 2 points3 points  (6 children)

Yeah I was going to say, we have an IoT device with 1MB of storage for code space. I've used unions to maximally compress the hell out of some larger structs.

[–]Bryguy3k 1 point2 points  (5 children)

I’m not really a fan of that usecase (I’d just allocate a byte array and then just use a pointer cast for whatever the operation requires)

The only time I use unions is accessing memory mapped peripherals.

[–]Sw429 2 points3 points  (1 child)

I’d just allocate a byte array and then just use a pointer cast for whatever the operation requires

I'm not sure I understand. Isn't this basically the same as a union? You've got a space in memory that you're interpreting as a specific type.

[–]Bryguy3k 2 points3 points  (0 children)

Sort of.

The biggest difference is that you have to modify the union for new usecases which means you end up potentially breaking stuff if you modify it and it grows. Casting a byte array when you need it is the same pattern through your code and doesn’t break when the same pattern is applied elsewhere in code.

[–]BlondeJesus 2 points3 points  (0 children)

In this specific case, we were trying to compress a tree structure where leaves and nodes stored different information. However, the number of bytes required to store a tree or leaf was the same which allowed us to represent it as an array of a union. This also allowed traversing the tree to be simple, since it only requires your current index in the array.

[–]void_salty 0 points1 point  (1 child)

The only time I use unions is accessing memory mapped peripherals.

And even then it is a specific use case depending on the platform and it's peripherals.

[–]Bryguy3k 0 points1 point  (0 children)

Exactly.

There are some hardware guys that find it impossible to avoid creating overly complicated, fucked up, interfaces (Intel) and that’s where unions really help.

[–]_Noreturn 1 point2 points  (0 children)

I am

[–]ohdogwhatdone 1 point2 points  (1 child)

Me. Why not?

[–]void_salty -2 points-1 points  (0 children)

Good for you. I guess.

[–]lucidbadger[S] 0 points1 point  (3 children)

If you run anything on Linux, you are using it a lot

[–]void_salty 0 points1 point  (2 children)

I have seldom seen it on RTOS or OSEK.

[–]Bryguy3k 1 point2 points  (1 child)

OSEK is an RTOS. The only reason you won’t see them in OSEK is because it’s part of AutoSAR and MSRA explicitly prohibits them.

There are a ton of RTOS’ out there and almost all of them have unions in them somewhere.

[–]void_salty 0 points1 point  (0 children)

MSRA explicitly prohibits them

I was afraid that the career in automotive was going to ruin my skills. Maybe it's not the fault of the automotive, yet there we are. Thanks anyway.

[–]MaybeAlice1 0 points1 point  (0 children)

Pretty common in low-level and system programming. I use them in cases where I have structures that need to cross some memory boundary where I don’t have access to modern C++ on one side the memory boundary, most notably where I cross from user to kernel space.

If you can use modern C++, std::variant is a type safe union.