Casting buffer pointer to struct

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
AnCi_2234676
Level 4
Level 4
10 replies posted 5 replies posted 10 questions asked

I want to cast a byte array pointer to the following structure:

typedef struct {

    uint16_t    data1;

    uint16_t    data2;

    uint16_t    data3;

    uint16_t    data4;

    uint8_t     data5;

} BleServicesValues_t;

I do something like this:

void func1(uint8_t* buffer) {

    BleServicesValues_t* values;

    values = (BleServicesValues_t*) &buffer[1];

    func2(values->data1);

}

While debugging, everything looks fine, but when I step into func2, the program jumps to Fault_Handler in startup_psoc63_cm0plus.S.

Does anyone understand why this is not working?

0 Likes
1 Solution
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

That's an alignment problem. The BleServicesValues_t cast should be set to an address that is multiple of 4 (32-bit architecture). You are probably accessing an address ending to 1.

If you cast at buffer[0] you probably be fine.

View solution in original post

1 Reply
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

That's an alignment problem. The BleServicesValues_t cast should be set to an address that is multiple of 4 (32-bit architecture). You are probably accessing an address ending to 1.

If you cast at buffer[0] you probably be fine.