Search code examples
cavrbootloaderatmel

((void(*)(void))0)() is Exit function?


I was trying to write my own boot loader on Atmel AVR Microcontroller. I have referred one of the code base from github. I would like to thank for the code base to ZEVERO

At primary level I understand the code base. but at line 224 I found a line Reference to the code

**if (pgm_read_word(0) != 0xFFFF) ((void(*)(void))0)();   //EXIT BOOTLOADER**

I understand the if condition part but when I was trying to understand the true statement part i.e.

**((void(*)(void))0)();**

code writer has given explanation to this is //EXIT BOOTLOADER

My first Question is what is the meaning of this complex declaration **((void(*)(void))0)();**

And Second Question is, does it Exit the execution of the code in Microcontroller.


Solution

  • As @iBug pointed out, ((void(*)(void))0)(); invokes a function call on a NULL function pointer.

    In effect, that transfers program control to memory address 0. Now, on a workstation, that would be colossal UB, most likely resulting in a segfault.

    However, since the code in question is for a hardware bootloader, it's not UB, it (apparently) just exits the bootloader.

    At the hardware level, almost everything is implementation dependent, and almost nothing is portable. You can't expect C code targeted at a specific hardware platform to be in any way representative of generally-accepted C patterns and practices.