edA-qa mort-ora-y via llvm-dev
2018-Apr-08 09:46 UTC
[llvm-dev] Does an array and homogeneous struct have the same layout?
Does an array of a value type and a structure containing only that type have the same alignment requirements? That is, in C-syntax: float* array; struct point { float x; float y; } point p; array = (float*)&p; Does `array[0]` now refer to x, and `array[1]` to y? I'm creating these structures in LLVM with default packing (StructType::isPacked == false). I have a library that takes simple arrays for point structs (OpenGL) and would like to layer nicer types on top. I'm reasonably certain the above holds true, but want to double-check that it's guaranteed. I'd also be doing this type of operation to refer to a list of points: point* p = ...; array = (float*)p; -- edA-qa mort-ora-y http://mortoray.com/ Creator of the Leaf language http://leaflang.org/ Streaming algorithms, AI, and design on Twitch https://www.twitch.tv/mortoray Twitter edaqa
Tim Northover via llvm-dev
2018-Apr-08 10:15 UTC
[llvm-dev] Does an array and homogeneous struct have the same layout?
On 8 April 2018 at 10:46, edA-qa mort-ora-y via llvm-dev <llvm-dev at lists.llvm.org> wrote:> Does an array of a value type and a structure containing only that type > have the same alignment requirements? That is, in C-syntax:Yes, they're both derived from the DataLayout in a way that targets cannot override.> Does `array[0]` now refer to x, and `array[1]` to y?Aside from array[1] being undefined behaviour n C, yes. The equivalent is fine in LLVM of course, where types are punned merrily. Cheers. Tim.
Maybe Matching Threads
- A struct {i8, i64} has size == 12, clang says size 16
- A struct {i8, i64} has size == 12, clang says size 16
- A struct {i8, i64} has size == 12, clang says size 16
- A struct {i8,i64} has size == 12, clang says size 16
- Migration from 3.8 to 6.0 questions (segfault most concerning)