在MSP430上,当我取消引用空指针时会发生什么?

Nic*_*ick 5 c pointers msp430 null-pointer iar

我知道取消引用空指针是未定义的 - 但我想知道在特定目标上发生了什么 - 一个MSP430.

我现在没有一块板可以在我面前加载它来测试它.

如果我这样做(或者类似的话)会发生什么?

int * foo = NULL;
(*foo)++; //Crash?
Run Code Online (Sandbox Code Playgroud)

位置0x0在SFR范围内并保留.

它会产生PUC/POR吗?还是会默默地"工作"?

生成的程序集是

;int * foo = NULL;
clr.w   R15
;(*foo)++;
inc.w   R15
Run Code Online (Sandbox Code Playgroud)

因此,位置0x0实际上是递增1.

当我在模拟器中运行它时,我看到地址0x0的值从0变为1.我在调试日志中没有警告,程序正常退出.

我正在使用IAR EW430编译器/汇编器/模拟器.

kfx*_*kfx 6

Not only writing and reading the address 0x0 will not cause a crash or a reboot, it actually a completely legal operation that is often used by MSP430 applications.

The initial portion or MSP430 memory map is reserved for I/O ports and control registers: http://en.wikipedia.org/wiki/TI_MSP430#MSP430_address_space

In particular, the control registers at 0x0 and subsequent addresses are:

 #define IE1_                  0x0000    /* Interrupt Enable 1 */
 #define IE2_                  0x0001    /* Interrupt Enable 2 */
 #define IFG1_                 0x0002    /* Interrupt Flag 1 */
 #define IFG2_                 0x0003    /* Interrupt Flag 2 */
Run Code Online (Sandbox Code Playgroud)

So for example writing zero to that memory address by dereferencing a uint8_t * or uint16_t * pointer is going to disable interrupts. Writing zero by dereferencing an uint32_t * it is also going to clear the flags. Incrementing the value of these registers does not make a lot of sense, but should be completely legal.

至少在msp430系列1,系列2和系列4上就是这种情况.通过检查头文件,我无法在系列5上找到映射到0x0的任何内容(中断控制寄存器映射到从0x0100开始的区域).

因此,如果你想在代码中捕获NULL指针被解除引用的位置,那么你完全靠自己.