取消引用一个NULL指针"?;究竟由&QUOT意思 [英] What EXACTLY is meant by "de-referencing a NULL pointer"?
问题描述
我是一个完整的新手到C,和我的大学工作中我遇到在code评论说,往往是指取消引用一个NULL指针。我有在C#中的背景下,我已经得到通过,这可能是类似于的NullReferenceException你是在净得到的,但现在我有严重的怀疑。
I am a complete novice to C, and during my university work I've come across comments in code that often refer to de-referencing a NULL pointer. I do have a background in C#, I've been getting by that this might be similar to a "NullReferenceException" that you get in .Net, but now I am having serious doubts.
有人请给我解释一下在深入浅出而言究竟这是什么,以及为什么它是坏的?
Can someone please explain to me in laymans terms exactly what this is and why it is bad?
推荐答案
A NULL
指针指向一个不存在的内存。这可能是地址 00000000
或任何其他实现定义值(只要它不能是一个真正的地址)。解除引用意味着试图访问无论是由指针指向的。在 *
运算符是对其操作:
A NULL
pointer points to memory that doesn't exist. This may be address 0x00000000
or any other implementation-defined value (as long as it can never be a real address). Dereferencing it means trying to access whatever is pointed to by the pointer. The *
operator is the dereferencing operator:
int a, b, c; // some integers
int *pi; // a pointer to an integer
a = 5;
pi = &a; // pi points to a
b = *pi; // b is now 5
pi = NULL;
c = *pi; // this is a NULL pointer dereference
这正是在C#同样的事情,作为一个的NullReferenceException
,除了在C指针可以指向任何数据对象,数组内的偶数元素。
This is exactly the same thing as a NullReferenceException
in C#, except that pointers in C can point to any data object, even elements inside an array.
这篇关于取消引用一个NULL指针"?;究竟由&QUOT意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!