How to do proper alignment in structs?

This “Harleya” guy seems to be dispensing bad advice. LOL

I never look at advice or any information as being absolute anyway. I’m just glad to receive help

1 Like

Ok. I now know how to do proper alignment and padding within Blender. I’m planning on writing a little tutorial for others who might be interested. There are a number of useful and or necessary things to understand in order to do proper alignment.

After I get it wrote, I hope maybe some more experienced people can correct and add to it to improve it. I’m gonna shoot to have it included in the official documentation pages too (I know there’s already a struct alignment section, but I think it needs to be improved).

Reading an error message and fixing the problem needs a tutorial?

Don’t get me wrong, anyone wanting to write documentation i’m not gonna discourage,

but yikes…

A very small tutorial, yeah. I was mostly just planning on making a few modifications to the current documentation.

Well, consider, for example, I don’t remember the Blender documentation mentioning two dimensions of alignment. The entirety of the struct and also the individual members. That’s just one thing that needs to be addressed.

but makesdna tells you exactly what is wrong and how to fix it, even if you know none of the rules…

7>Align 4 error (32 bit) in struct: TreeFilterElement i (add 1 padding bytes)

Add one padding byte before i

7>Sizeerror 4 in struct: TreeFilterElement (add 3 bytes)

Add 3 padding bytes at the end of the structure.

the only thing confusing is that the 3 at the end, will be 2 once you fix the i alignment, but really… makesdna is doing a pretty good job at holding your hand here

That’s actually what I was wanting to know from my very first post, but I still think that the documentation needs to be moderately improved. I know because I’ve read it. Actually, the simple instructions on reading makesdna.exe error message might be good to include in the documentation also.

Like i said, not gonna stop anyone from writing documentation, more is always welcome, I’m just surprised that ‘reading an error message that unambiguously spells out what is wrong and how to fix it’ is one of the things we need to improve the documentation on.

@Harleya, apparently, didn’t know that position of padding mattered. He seems seasoned enough that he should have.

I’ll be back with an improved documentation version.

I am well-seasoned to the point of being too salty, but there is no limit to the things I don’t know yet, especially with C, but I am fine with that. Never stop learning.

Regarding documentation, I think it would be wonderful for anyone new to this to do so. So many things are so obvious once you know them but so mystifying when you don’t. And we tend to just run past these learning moments and forget to help those who come after us.

For this particular issue, I have never actually noticed the type of error message you posted. Generally I do something wrong and get a nice error in MSVS telling me I am an idiot. But if it doesn’t compile and the error output makes no sense at all then I just know that I have screwed up an alignment somewhere.

1 Like

Lol, actually, after @LazyDodo explained how to read the makesdna.exe error message(output to VS log window) – everything became all too obvious.

After looking again at the current Blender alignment documentation I realized the SDNA Notes link didn’t work, and after googling that page I found that most everything I wanted to point out was already documented. So, I don’t know if I’ll even bother with improving the docs.

Maybe, maybe not.

I say you still have a stab at it, this is not the first time this question came up . And i’m still clearly confused why people are not getting what is wrong. Perhaps instead of better docs, is there anything in the error message coming out of makesdna you may want to improve?

Well, for me, I have a mental fog disorder, and also the problem was partly not properly understanding how padding and alignment worked – particularly that alignment has two dimensions to consider. So, I don’t suppose people would grasp the error message without understanding alignment first.

As far as what could be improved in the error message… I would say it should more specifically reflect just what you yourself wrote to me.

Example:

7>Align 4 error (32 bit) in struct: (before) TreeFilterElement i (add 1 padding bytes)
7>Sizeerror 4 in struct: (at end of) TreeFilterElement (add 3 bytes - [Align 4 error])

.
Example 2:

7>Align 4 error (32 bit) in struct: TreeFilterElement i (to resolve add 1 padding bytes; before element i  )
7>Sizeerror 4 in struct:  TreeFilterElement (to resolve add 3 padding bytes - [Align 4 error]; to end of struct TreeFilterElement)

so instead of

7>Align 4 error (32 bit) in struct: TreeFilterElement i (add 1 padding bytes)

something like

7>Align 4 error (32 bit) in struct: TreeFilterElement i (to resolve add char _pad[1]; before element i)

?

but at that point it’s gonna be confusing cause _pad may be taken…

That sounds really nice. And the _pad[1] issue can easily be resolved with something like _pad?[1] or any number of alternatives.

I am confused about why I am not seeing the error as described.

I just tried to simulate this. I added 4-byte member to the ThemeSpace struct (DNA_userdef_types.h). Before doing so I get no errors at all. After doing so I am shown 113 errors, none that seems to point near my error:

But, as mentioned, it is precisely this behavior that I now recognize as “I must have misaligned something”. LOL

msvc only looks for errors that are formatted out in it’s own special syntax, the error is visible in your output window (view -> Output or Ctrl-W O) we could detect we’re building with msvc and tweak the output so it would be visible in this window as well.

also, bf_dna has clearly failed, the fact that it still happily tries to build bf_blenloader annoys me, but unsure how to fix that.

Ah… makes sense. I wouldn’t have thought to look at the Output window once getting so many errors.

we could do something like this

but it’s gonna require a little bit of restructuring of makesdna, since it be reaaaaaaal nice to have the file/line number information available.

3 Likes

Damn, that is beautiful…