空函数指针 [英] NULL function pointers
问题描述
什么是调用一个空函数指针的行为?
What is the behavior of calling a null function pointer?
void (*pFunc)(void) = NULL;
pFunc();
为什么是明智的初始化尚未使用的函数指针为NULL?
Why is it advisable to initialize yet unused function pointers to NULL?
推荐答案
在C和C ++,这就是所谓的未定义行为
,这意味着这可能导致的分段故障的任何这样的情况下,会造成基于你的编译器,你在运行此code操作系统,环境(等),没有或手段。
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.
初始化一个指向函数的指针,或一般为NULL指针帮助一些开发商以确保他们的指针未初始化的并且不等于一个随机值,从而$ P $ pventing他们意外取消引用它。
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.
这篇关于空函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!