c# - Can we make assumptions over structs layouts? -
is there convention on algorithm used make layouts of structs on c?
i want able have code running in vm able have structures compatible c counterparts, c# interop works. need know how alignment algorithm works. gather there must convetion that, works nicely on c#. have in mind probable algorithm have used work out, haven't found proof right one.
here's how think works:
for each declared field (by order of declaration)
- see if field fits in remaining bytes (until next alignment)
- if doesn't fit, align field; otherwise add current offset
for example, on 32-bit system struct like:
{ byte b1; byte b2; int32 i1; byte b3; }
would this algorithm:
{ byte b1; byte b2; byte[2] align1; int32 i1; byte b3; byte[3] align2; }
in general, structure alignment in c depends on compiler used, , especially compiler options in effect @ time structure declaration processed. can't make general assumptions except particular structure in particular program compiled particular settings, structure layout can determined.
that said, guess closely matches most compilers likely default alignment settings.
Comments
Post a Comment