Given below a small piece of code from an open source project,
Can you interpret above code?
It is simple, a step by step approach is given below,
The statement while(false) is meant only for testing purpose. Consider the following operation,
It can be achieved as follows,
0; /* literal zero */ (0); ( ()0 ); /* 0 being casted to some type */ ( (*) 0 ); /* 0 casted some pointer type */ ( (*)() 0 ); /* 0 casted as pointer to some function */ ( void (*)(void) 0 ); /* Interpret 0 as address of function taking nothing and returning nothing */ ( void (*)(void) 0 )(); /* Invoke the function */
So the given code is invoking the function whose code is stored at location zero, in other words, trying to execute an instruction stored at location zero. On systems with memory protection (MMU) the OS will throw an exception (segmentation fault) and on systems without such protection (small embedded systems), it will execute and error will propagate further.
— Venki. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- How to print a variable name in C?
- A C Programming Language Puzzle
- Arrays in C/C++
- Function Pointer in C
- Diffference between #define and const in C?
- Interesting Facts about Macros and Preprocessors in C
- Branch prediction macros in GCC
- Variable length arguments for Macros
- Multiline macros in C
- return statement vs exit() in main()
- puts() vs printf() for printing a string
- The OFFSETOF() macro
- Do not use sizeof for array parameters
- Write a C macro PRINT(x) which prints x