If I create or add a member to a struct in a DNA_*.h file I will usually get alignment error. Apparently, two aspects of alignment must be dealt: position of padding within the struct and number of bytes within the padding array. So far, I’ve had to use the trial and error method to find out when my padding resolves the problem. This, however, is tedious.
Question: What is the common way to find out where padding should go, and how many bytes should the padding array contain ?
Question: Is the alignment error below actually instructing the proper location and number of bytes for proper padding, and if so, can you explain the instructions ?
7>Align 4 error (32 bit) in struct: TreeFilterElement i (add 1 padding bytes)
7>Align 4 error (64 bit) in struct: TreeFilterElement i (add 1 padding bytes)
7>Align 2 error (32 bit) in struct: TreeFilterElement s (add 1 padding bytes)
7>Align 2 error (64 bit) in struct: TreeFilterElement s (add 1 padding bytes)
7>Sizeerror 4 in struct: TreeFilterElement (add 3 bytes)
7>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(209,5): error MSB6006: "cmd.exe" exited with code 1.
7>Done building project "bf_dna.vcxproj" -- FAILED.
Here is my test struct that I’d like to understand:
AFAIK you only need to use padding to ensure that the total size of the structure is a multiple of 8 bytes. So to make it is exactly 8, 16, 24, 32, 40, 48, etc. I have never come across a time when the position of the padding made any difference.
Hmm. I tried adding char array elements one at a time per rebuild and I simply could not get an error free build until I actually tested changing positioning of padding. It meant the difference between error and no error. So I don’t know what that was about. Of course, this was for struct SpaceOutliner
Yes, there is some quasi-inheritance there. All the “space*” structs start with the stuff that is in the struct SpaceLink, which itself has padding. So you can’t piss around with the stuff in there - until after the “End ‘SpaceLink’ header” comment. So don’t touch the header and just add/remove padding after that to align it all to 8 byte boundaries.
So, is the error message that I listed produced by makesdna.exe rather than VS ? Or maybe you’re referring to makesdna.exe error logs?
If so, you wouldn’t happen to know how to interpret and apply the error message to properly correct for alignment - would you ?
Obviously, the error message tells me alignment is off, but I don’t know what to change from that point. It seems like there are usually three spots that typically may need padding for any one member addition or removal.
I’m not positive for all x86/x64 details, but in general in RISCy architectiures (or things like x86/x64 that have RISCy implementations these days) you want things to be aligned to match their size, so characters can be anywhere, shorts need to be at even byte addresses (multiples of 2), ints at a multiple of 4, and longs/pointers at a multiple of 8 (on an I32LP64 architecture/mode).
I don’t know how much Blender has separate cases for 32-bit builds or whether it just leaves the extra 64-bit padding in and it all works out automagically when pointers are only 4 bytes instead of 8.
That’s what I was reading. But I would like to get some professional advice on any details about how to make the process simpler and more intuitive. For one thing/problem, if you declare a struct member inside a struct, you have to somehow figure out how big the struct variable is, and that adds to the tediousness. Actually, it may be that my entire problem is just pointer related though. I may even simply need to just think about alignment a lil more. I’ll test tomorrow
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).
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.