简体   繁体   中英

Pointer casting problem in C with Atollic TrueSTUDIO for STM32 IDE

Working on STM32H7 in Atollic TrueSTUDIO for STM32 IDE. Only C coding. Using FreeRTOS.

Ui08 *pointerSomething;
Ui64 localVariable;

pointerSomething=&addressOfSomething;
localVariable = *(Ui64*)(pointerSomething);

These code is generally working.

But one of my usage in a case in a thread in something like that;

thread begin //

Ui08 *pointerSomething;
Ui64 localVariable;

case 3: 

   pointerSomething=&addressOfSomething;
   localVariable = *(Ui64*)(pointerSomething);

break;

thread end //

And I am getting a hardfault when the second sequence in these case. I mean first time in case working properly but second time in case getting hardfault exactly the line of localVariable = *(Ui64*)(pointerSomething);

thread begin //

Ui08 *pointerSomething;
Ui64 localVariable;

case 3: 

   pointerSomething=&addressOfSomething;
   memcpy( &localVariable, pointerSomething, sizeof(localVariable) );

break;

thread end //

If I change these line as you can see above, the problem is fixing for all time of case. But my question is why this problem is occuring, casting type of line?

There is nothing to guess here.

gcc is compiling for Cortex-M7 (thumb) 64-bit pointer pun to the LDRD instruction. LDRD instruction requires aligned addresses. That is the reason why you are getting hardFaults from time to time when the address is not word aligned.

https://godbolt.org/z/o9sPvfaon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM