Skip to content

Commit 0eade62

Browse files
committed
Add packed structure documentation
1 parent d5c4628 commit 0eade62

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

doc/c.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,27 @@ Because Spin and BASIC are case insensitive languages, their identifiers may be
255255

256256
Class support for C/C++ is very much incomplete, and probably will not work in all cases. In particular, calling functions outside of the class may not work (so simple self contained classes should be fine, but classes which call into other classes may not work properly).
257257

258+
### Packed structures
259+
260+
Like GCC, FlexC supports packing of structures. Normally, variables within structures are aligned on natural boundaries, to improve the speed of access. So for example the structure:
261+
```
262+
struct mystruct {
263+
unsigned char a;
264+
long b;
265+
} X;
266+
```
267+
has the field `a` at offset 0 and `b` at offset 4, with padding between the two.
268+
However, on the P2 it is possible to remove this padding by adding the tag `__attribute__((packed))` after the `struct` keyword:
269+
```
270+
struct __attribute__((packed)) mystruct {
271+
unsigned char a;
272+
long b;
273+
} Y;
274+
```
275+
This time the offset of `b` is 1, and no extra padding is introduced. This will only work on P2, where the hardware supports unaligned accesses (with a small performance penalty).
276+
277+
Note that while the `__attribute__` keyword may be accepted in other places in variable definitions, placing it immediately after the `struct` keyword is the only officially supported placement right now (other places may or may not work properly).
278+
258279
### Header file external function definitions
259280

260281
There is no linker as yet, so in order to use standard library functions we use a FlexC specific construct, `__fromfile`. The declaration:

0 commit comments

Comments
 (0)