NULL function pointers

后端 未结 3 1895
野性不改
野性不改 2021-01-17 11:04

What is the behavior of calling a null function pointer?

void (*pFunc)(void) = NULL;  
pFunc();

Why is it advisable to initialize yet unu

3条回答
  •  清酒与你
    2021-01-17 11:38

    In C and C++, this is called undefined behaviour, meaning that this can lead to a Segmentation fault, nothing or whatever such a case will cause based on your compiler, the operating system you're running this code on, the environment (etc...) means.

    Initializing a pointer to a function, or a pointer in general to NULL helps some developers to make sure their pointer is uninitialized and not equal to a random value, thereby preventing them of dereferencing it by accident.

提交回复
热议问题