Skip to main content
Ellaine
Associate II
Jul 10, 2020 6:00 PM UTC
Solved

Fault when casting from char* to uint64_t*

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);

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

​

    This topic has been closed for replies.
    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    Jul 10, 2020 6:11 PM UTC

    >>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 (See Profile) Up vote any posts that you find helpful, it shows what's working..

    2 replies

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    Jul 10, 2020 6:11 PM UTC

    >>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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Ellaine
    EllaineAuthor
    Associate II
    Jul 10, 2020 6:25 PM UTC

    Ok, thanks.