__packed typecast

Hello all,

Previously i was using MDKARM 4.0 and now i've upgraded to the 4.7. I am writing the program for LPC17xx. In the previous version i was using unsigned long long data type for 64 bit data storage. When i want to store the data in buffer i was using following

 *(unsigned long long *)buf->dptr   = n->ex;

where ex is __packed unsigned long long data type.

I used __packed because of the data alignment boundary.

Now when i am upgraded to 4.7 same instruction gave me unaligned fault, may be because of some changes in compiler. Just out of the hunch i changes above statement to following

 *(__packed unsigned long long *)buf->dptr   = n->ex;

Now there is now unaligned fault. I just wanted to ask if it is the correct way or it may lead to unexplained behavior in future. If not what could be the correct way to typecast. Please point me in correct direction.

Thanks in advance
Ra

Parents
  • where ex is __packed unsigned long long data type.

    It is almost certainly quite irrelevant what "ex" is. The problem is on the left hand side of that line.

    may be because of some changes in compiler.

    Only indirectly. The error is because that line of code has been wrong all along --- that __packed keyword was strictly necessary. If it appeared to work as desired before, that was most likely by pure coincidence.

    You cannot cast up an arbitrary pointer to a pointer type with alignment requirements as high as those of a 64-bit type on a 32-bit ARM, and expect it to just work. Not unless you're absolutely sure that the value of dptr is sufficiently aligned, that is.

    You would have been better off spelling that as

    memcpy(buf->dtr, n->ex, sizeof(unsigned long long));
    

Reply
  • where ex is __packed unsigned long long data type.

    It is almost certainly quite irrelevant what "ex" is. The problem is on the left hand side of that line.

    may be because of some changes in compiler.

    Only indirectly. The error is because that line of code has been wrong all along --- that __packed keyword was strictly necessary. If it appeared to work as desired before, that was most likely by pure coincidence.

    You cannot cast up an arbitrary pointer to a pointer type with alignment requirements as high as those of a 64-bit type on a 32-bit ARM, and expect it to just work. Not unless you're absolutely sure that the value of dptr is sufficiently aligned, that is.

    You would have been better off spelling that as

    memcpy(buf->dtr, n->ex, sizeof(unsigned long long));
    

Children
More questions in this forum