cancel
Showing results for 
Search instead for 
Did you mean: 

Fault when casting from char* to uint64_t*

Ellaine
Associate II

In my code, I write many casting from char* to uint64_t*. Someone success, and someone get failure. I thought I used the unallocated memory, but I rewrote by casting to uint32_t, and then no error happened. Is this normal?

My code:

--------------------------------------

 uint32_t sz_t, sz_h;

 sz_t = ((uint32_t*)bptr)[0];

 bptr += sizeof(sz_t);

 sz_h = ((uint32_t*)bptr)[0];

 bptr += sizeof(sz_h);

-----------------------------​--------

1 ACCEPTED SOLUTION

Accepted Solutions

>>Is this normal?

Yes, LDRD/STRD instructions will fault on unaligned (32-bit) reads, so can't randomly cast pointers safely. Problem for uint64_t and doubles, most frequently.

Typical method in ARM's old and new, is to memcpy() to an aligned buffer and read out of that.

The Cortex-M0(+) is even more unforgiving 32-bit reads can't be misaligned.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2

>>Is this normal?

Yes, LDRD/STRD instructions will fault on unaligned (32-bit) reads, so can't randomly cast pointers safely. Problem for uint64_t and doubles, most frequently.

Typical method in ARM's old and new, is to memcpy() to an aligned buffer and read out of that.

The Cortex-M0(+) is even more unforgiving 32-bit reads can't be misaligned.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Ellaine
Associate II

Ok, thanks.