aligning_20structures
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
aligning_20structures [2018/03/31 13:19] – external edit 127.0.0.1 | aligning_20structures [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 4: | Line 4: | ||
==== Method 1 ==== | ==== Method 1 ==== | ||
\\ This involves two steps, firstly adding some dummy //padding// to the end of the structure so that there is room to move its data (if necessary), and secondly to adjust the structure' | \\ This involves two steps, firstly adding some dummy //padding// to the end of the structure so that there is room to move its data (if necessary), and secondly to adjust the structure' | ||
+ | <code bb4w> | ||
DIM mystruct{member1, | DIM mystruct{member1, | ||
- | | + | |
+ | </ | ||
When the structure is declared, a dummy member **padding%** is added at the very end; this allows the structure to be moved up in memory by 4 bytes, if necessary, without corrupting subsequent memory contents; //it is vitally important that this member is never written//! The second line adjusts the address of the structure so that it is a multiple of eight.\\ \\ If you want to align the structure at a multiple-of-16 address the padding needs to be 12 bytes in size, most easily arranged by making it an array: | When the structure is declared, a dummy member **padding%** is added at the very end; this allows the structure to be moved up in memory by 4 bytes, if necessary, without corrupting subsequent memory contents; //it is vitally important that this member is never written//! The second line adjusts the address of the structure so that it is a multiple of eight.\\ \\ If you want to align the structure at a multiple-of-16 address the padding needs to be 12 bytes in size, most easily arranged by making it an array: | ||
+ | <code bb4w> | ||
DIM mystruct{member1, | DIM mystruct{member1, | ||
- | | + | |
+ | </ | ||
\\ | \\ | ||
==== Method 2 ==== | ==== Method 2 ==== | ||
\\ With this method there is no need to add any padding to the structure, but the original structure' | \\ With this method there is no need to add any padding to the structure, but the original structure' | ||
+ | <code bb4w> | ||
DIM mystruct{member1, | DIM mystruct{member1, | ||
DIM newdata% DIM(mystruct{})+6 | DIM newdata% DIM(mystruct{})+6 | ||
- | | + | |
+ | </ | ||
Similarly to align it on a multiple-of-16 address: | Similarly to align it on a multiple-of-16 address: | ||
+ | <code bb4w> | ||
DIM mystruct{member1, | DIM mystruct{member1, | ||
DIM newdata% DIM(mystruct{})+14 | DIM newdata% DIM(mystruct{})+14 | ||
- | | + | |
+ | </ | ||
Note that you must declare the structure using this method **only once** and not repeat the above code (for example in a loop).\\ \\ If declaring a structure for local use in a procedure or function, use **DIM LOCAL**: | Note that you must declare the structure using this method **only once** and not repeat the above code (for example in a loop).\\ \\ If declaring a structure for local use in a procedure or function, use **DIM LOCAL**: | ||
+ | <code bb4w> | ||
LOCAL mystruct{}, newdata% | LOCAL mystruct{}, newdata% | ||
DIM mystruct{member1, | DIM mystruct{member1, | ||
DIM newdata% LOCAL DIM(mystruct{})+6 | DIM newdata% LOCAL DIM(mystruct{})+6 | ||
- | | + | |
+ | </ |
aligning_20structures.1522502344.txt.gz · Last modified: 2024/01/05 00:18 (external edit)