Happy New Year!
This is a popular recipe for embedded (pure) C, to reset device:
void reset()
{
((void(*)(void))0)();
};
Looks creepy!
It's easier to understand it if to compile.
GCC 14.2 non-optimized:
reset:
endbr64
push rbp
mov rbp, rsp
mov eax, 0
call rax
nop
pop rbp
ret
GCC 14.2 optimized:
reset:
endbr64
xor eax, eax
jmp rax
Many embedded CPUs starts at 0th address upon power on or reset. Here we simply jump to address 0, where (presumably) firmware is located.
That pure C code simply calls a function by pointer. There is no (portable) way in pure C to jump to 0th address, but we can call some 'function' there. What about stack? Local variables? All that is dropped by firmware code at 0th address.
void(*)(void) is type of function pointer, that accepts no arguments (void) and return no result (void again).
(void(*)(void))0 is zero address is casted to that type.
((void(*)(void))0)(); is function call.
Bottom line - sometimes compilation can help in understanding such tricks.
