struct aligment issue

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
jorsc
Member++
Posts: 21
Joined: Fri May 05, 2017 1:39 pm

struct aligment issue

Post by jorsc »

I've have an issue with structs not being aligned correctly. In my program this resulted in the runtime error:
"xrun: Program received signal ET_LOAD_STORE, Memory access exception."

As I understand it is convention on 32bit platforms that int members and struct sizes should be aligned to 4 bytes boundaries. This mostly seems to be the case with xc, However consider this code:

Code: Select all

#include <stdint.h>
#include <stdio.h>
#include <xassert.h>

typedef struct {
	int8_t a;
	uint32_t b;
	int8_t c;
} test1_t;

typedef struct {
	uint8_t a;
	uint16_t b;
	uint8_t c[1536];
	uint16_t d;
	// uint8_t padding[2]; // fixes issue but should be inserted by compiler
} test2_t;

void testAligment(void) {
	test1_t t1;
	test2_t t2;
	xassert(&t1.a + 4 == &t1.b); // test padding of int b to 4 byte alignment: OK
	xassert(sizeof(t1) % 4 == 0); // test padding of struct size t1 to 4 byte alignment: OK
	xassert(sizeof(t2) % 4 == 0); // test the same for t2: FAILS!
}
Compiler bug?, I'm I missing something?


User avatar
CousinItt
Respected Member
Posts: 360
Joined: Wed May 31, 2017 6:55 pm

Post by CousinItt »

Post Reply