Skip to main content
Ellaine
Associate II
July 10, 2020
Solved

Fault when casting from char* to uint64_t*

  • July 10, 2020
  • 2 replies
  • 2222 views

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.
    Best answer by Tesla DeLorean

    >>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.

    2 replies

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    July 10, 2020

    >>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 VenmoUp vote any posts that you find helpful, it shows what's working..
    Ellaine
    EllaineAuthor
    Associate II
    July 10, 2020

    Ok, thanks.