4 ms·
Doesn't this violate strict aliasing?
by kopecs 3y ago
Doesn't this violate strict aliasing?
- cozzyd 3y agoyes, though you can fix that with compiler flags (or, #pragma if you want strict aliasing elsewhere in your code) alternatively, gcc supports VLAs in unions, but I don't think clang does, but that makes it extra annoying to do. edit: apparently you can probably apply the may_alias attribute to the type? Or you could try using transparent_union. No idea if clang supports either...
- mananaysiempre 3y agoAt that point, it is IMO better to obtain that block from alloca()[1]—it’s not standard, but where it’s available the compiler will treat the result as untyped for the purposes of aliasing. (If you’re on GCC/LLVM, __builtin_alloca_with_align is also an option, although note that the memory it returns may not outlive the current block—similar to a standard automatic variable, but unlike memory from traditional alloca.) ISO C has a gigantic hole when it comes to obtaining and recycling untyped memory, pretending the hole is not there isn’t going to help. [1] https://nullprogram.com/blog/2019/10/28/ https://nullprogram.com/blog/2019/10/28/, discussed at the time at https://news.ycombinator.com/item?id=21374863 https://news.ycombinator.com/item?id=21374863
- jenadine 3y agoAnd alignment?
- cozzyd 3y agoyes, you may need an alignas depending on platform (though you probably want it even if unaligned access is supported).
- kazinator 3y agoJust use alloca. // in your code, which you could write a helper macro for if you were so inclined opaque_foo_t *my_foo = alloca(opaque_foo_sz()); opaque_foo_init(my_foo);
- bensecure 3y agochar* can alias anything
- cozzyd 3y agoThe strict aliasing optimization in gcc in theory can cause problems (which is why many, including the Linux kernel, disable that)