Home >
other > Why in the STM32 use pointer assignment crash data?
Why in the STM32 use pointer assignment crash data?
Compiled code USES MDK5 and uvison5.23.0.0 compilation, C99mode, happened a strange phenomenon:
Global variable device_info [200]={0};
Temporary local variable uint32_t tp1=0 xffeeddc1;
To save the local variables with the 32 bit directly to the front of the array device_info 4 bytes, there are two ways,
Method 1:
Because device_info is a pointer to the first byte array, equal & amp; Device_info [0], so will the pointer cast to uint32_t * type,
And then through the new pointer directly modifying device_info [0, 3] 4 bytes,
* ((uint32_t *) device_info)=tp1.
Method 2:
In turn, because tp1 is a 32 bit number, occupy 4 bytes in memory, & amp; Tp1 is to point to the first byte of the 32 bit number address,
So the tp1 pointer cast to uint8_t * format, and then in turn address pointer + 1 and + 1, values of tp can be saved to device_info [0, 3],
(uint8_t device_info [0]=* (*) (& amp; Tp1));
(uint8_t device_info [1]=* ((*) (& amp; Tp1)) + 1);
(uint8_t device_info [2]=* ((*) (& amp; Tp1)) + 2);
(uint8_t device_info [3]=* ((*) (& amp; Tp1)) + 3);
Under Windows compilers, the two methods mentioned above are normal, but in MDK compiled, run into the STM32 and found that only 2 feasible method,
Use method 1 run * ((uint32_t *) device_info)=tp1 after this statement, microcontroller will crash,
Mystery, consult ace to give directions, thank you!