MM.
MM.

Reputation: 4274

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

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...."

Upvotes: 4

Views: 1864

Answers (9)

StoneCamel
StoneCamel

Reputation: 1

if fp is a pointer to a function, *fp is the function itself, so(fp)()is the way to invoke it. ANSI C permits this to be abbreviated as fp(), bu keep in mind that it is only an abbreviation. -------C traps an pitfalls. ( ( void()() )0 ) () is the avvreviation of ( ( void()() )0 )()

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

  • 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.

Upvotes: 3

Mike Dinescu
Mike Dinescu

Reputation: 55750

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.

Upvotes: 14

BoBTFish
BoBTFish

Reputation: 19767

It casts 0 to a function pointer, where the function takes no argument and returns void, then tries to call this function. It basically dereferences a null pointer.

Upvotes: 2

For me it is simpler to translate to a different C++, rather than directly to english:

typedef void (void_func_t)();       // type of a function taking no arguments 
                                    // and returning void
typedef void_fnct_t* void_func_ptr; // pointer to such a function
static_cast<void_func_ptr>(0)();    // call that function
// ((void_func_ptr)0)();            // pure C equivalent cast

Upvotes: 0

Jonathan Wood
Jonathan Wood

Reputation: 67251

It takes the value zero, and casts it to a function pointer that doesn't return anything (void).

Presumably, the purpose is that when you call this "function", it calls address zero, which should indeed crash the application.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

It casts a NULL pointer to a method taking no parameters and returning void, and attempts to call this method.

Needless to say, it crashes, so the name CRASH suits it very well.

Upvotes: 2

hamstergene
hamstergene

Reputation: 24439

It means “treating NULL pointer as pointer to void function(), call function()”.

Upvotes: 0

blueshift
blueshift

Reputation: 6882

It casts 0 to a pointer to a function, then attempts to call that function. Which will cause a segfault and crash.

Edit: way too much competition for these questions. Upvotes all round, and I'm going to bed :)

Upvotes: 0

Related Questions