Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the macro ((void(*)())0)() mean?

Tags:

c++

c

macros

The outcome of the following macro is clear:

#define CRASH() do {\
  *(int *)(uintptr_t)0xbbadbeef = 0;\
  ((void(*)())0)();\
} while (false)

My question is, what does the line

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

break down to, in English? For example, "this is a function that returns a pointer to a...."

like image 787
MM. Avatar asked Nov 30 '22 15:11

MM.


2 Answers

It looks like it casts 0 as a function pointer (with the signature that it takes not parameters and has void return type) and then invokes it.

(     (            void(*)()                  ) 0       )      ();
  /* cast..*/ /* fn pointer signature */  /*..cast 0 */  /* invocation */

Which is another way to say that it's trying to invoke (call) a function that's expected to be located in memory at address 0x00000000 - which is guaranteed to be an invalid address.

like image 149
Mike Dinescu Avatar answered Dec 05 '22 03:12

Mike Dinescu


  • Cast 0 to a pointer to a void function that takes can be called with no parameters (the (void(*)())0 part of the expression)
  • Call that function through a pointer with an empty parameter list (the () part after it).

EDIT 1: Edited in response to Cristoph's comment.

like image 42
Sergey Kalinichenko Avatar answered Dec 05 '22 02:12

Sergey Kalinichenko