“取消引用空指针"究竟是什么意思? [英] What exactly is meant by "de-referencing a NULL pointer"?

查看:43
本文介绍了“取消引用空指针"究竟是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全是 C 的新手,在我大学工作期间,我遇到过代码中的注释,这些注释通常涉及取消引用 NULL 指针.我确实有 C# 背景,我一直认为这可能类似于NullReferenceException";你进入 .Net,但现在我很怀疑.

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 layman's terms exactly what this is and why it is bad?

推荐答案

NULL 指针指向不存在的内存.这可能是地址 0x00000000 或任何其他实现定义的值(只要它永远不能是真实地址).取消引用它意味着尝试访问指针指向的任何内容.* 操作符是解引用操作符:

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.

这篇关于“取消引用空指针"究竟是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆