How to do proper alignment in structs?

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…

Yeah, that’s nice. It’d be nice to be able to see the align errors in the Error List window instead of swim through Output

Wait, what the heck is this error supposed to mean ?

1>------ Build started: Project: bf_dna, Configuration: Debug x64 ------
2>------ Build started: Project: bf_collada, Configuration: Debug x64 ------
3>------ Build started: Project: buildinfo, Configuration: Debug x64 ------
4>------ Build started: Project: locales, Configuration: Debug x64 ------
3>Generating buildinfo.h_fake, buildinfo.h
1>Generating dna.c, dna_type_offsets.h, dna_verify.c
1>Align pointer error in struct (size_native 8): TreeFilterElement *prop_val_comparator_max
1>Align pointer error in struct (size_64 8): TreeFilterElement *prop_val_comparator_max
1>Sizeerror 8 in struct: TreeFilterElement (add 2 bytes)
1>Sizeerror 4 in struct: TreeFilterElement (add 2 bytes)
1>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.
1>Done building project "bf_dna.vcxproj" -- FAILED.

My TreeFilterElement was already properly aligned, and I went ahead and tested commenting out char _pad1[6]; just to see what would happen. The error message seems to be saying that I only need to add 2 bytes to the struct. It neither gives the correct location nor the correct number of bytes to add. Of course, this is a “Align pointer error”.

Here’s my struct:

typedef struct TreeFilterElement {
  struct TreeFilterElement *next, *prev;
  ListBase *superlist, *sublist, *list;

  int apply_num_subtrees; 

  short struct_delete_switch;
  char struct_name_comparator[64]; 
  short prop_name_delete_switch;
  char prop_name_comparator[64]; 

  /* property value filter operation stuff */
  short prop_val_delete_switch;
  short fot;
  short op_min; 
  char _pad0[2];
  void *prop_val_comparator_min; 
  //char _pad1[6];    
  short op_max;
  void *prop_val_comparator_max;

  short elem_name_comparator_delete_switch;
  char elem_name_comparator[64];

  short proptype_delete_switch;
  short proptype_comparator;
  char _pad2[2];

} TreeFilterElement;

Align pointer error in struct (size_native 8): TreeFilterElement *prop_val_comparator_max

TreeFilterElement *prop_val_comparator_max needs to be aligned to 8 bytes, it’s currently not (but it’s not telling you how much to add, so you’ll either have to do the math, or just blindly try char _pad0[1];char _pad0[7]; one of them ough to do the trick.

1>Sizeerror 8 in struct: TreeFilterElement (add 2 bytes)
1>Sizeerror 4 in struct: TreeFilterElement (add 2 bytes)

The total size of the struct needs to be a multiple of both 4 and 8, add char _pad[2]; to the end of the structure. (but this is gonna change depending on the padding you add for the other field, so just fix one of them at a time.

1 Like

Well, that cleared that up.

Thanks

I explained it here as well, starting to wonder, are you just messing with me at this point?

What? No, your answer wasn’t for an “Align pointer error”. My last posted error and your answer was a little bit different than the other error and your answer.

I did not know that Sizeerror 8 ,Sizeerror 4 meant that the total size of the struct had to be a multiple of 8 and 4. Also, from my perspective, why would I easily understand that a lack of information in an error was simply just a lack of a developer bothering to write the error in makedna.exe ? Yeah, I would have eventually figured it out but it would be a lot better for a developer to just spill it plainly. Shoot, I would figure everything out eventually without asking questions.

Plus, I figure that this thread might as well be thorough for the next guy