空指针 [英] Null pointers

查看:53
本文介绍了空指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0x0可以是地址空间中的有效虚拟地址

应用程序吗?

如果它是有效的,那么NULL指针指向的位置

也是有效的,应用程序不应该在尝试读取

位置时收到SIGSEGV

(我说的是unix机器)。 />
然后我如何区分NULL指针和无效的
位置?

这是必不可少的,NULL指针不应该指向任何

虚拟地址空间中的位置?


如果NULL指针不应该基本上指向无效的

位置,那么为什么我们应该总是使用NULL作为位置0x0,

我们也可以使用其他位置吗?


当应用程序尝试访问时,这是必要的吗? >
位置由NULL指针指向,它应该由内核中止

发布一些信号?


请我需要你的帮助,是acuse我越来越困惑

因为我正在阅读越来越多关于NULL指针的文件。

thanx提前任何帮助.....

Can 0x0 be a valid virtual address in the address space
of an application ?
If it is valid, then the location pointed by a NULL pointer
is also valid and application should not receive "SIGSEGV"
( i am talking of unix machine ) while trying to read that
location.
Then how can i distinguish between a NULL pointer and an invalid
location ?
Is this essential that NULL pointer should not point to any of
the location in the virtual address space ?

If NULL pointer should not essentially point to an invalid
location, then why we should always use NULL to be location 0x0,
can we use some other location as well ?

Is this necessary that whenever an application try to access
location pointed by NULL pointer, it should be aborted by kernel
by posting some signal ?

Please i need your help, beacuse i am getting more confused
as i am reading more and more documents on NULL pointers.
thanx for any help in advance.....

推荐答案

junky_fellow写道:
junky_fellow wrote:
0x0可以成为应用程序地址空间中的有效虚拟地址吗?


是。

如果它有效,那么NULL指针指向的位置
也是有效的,应用程序不应该收到SIGSEGV ;
(我说的是unix机器),同时试图读取
位置。


编号如果0是机器上的有效(C可访问)地址,则

0不是指针值的合法表示。

然后,我如何区分空指针和无效的位置?


null(非NULL)指针测试等于整数常量

表达式,值为0.无效位置不要。

这是必不可少的,NULL指针不应该指向任何虚拟地址空间中的位置?


必须将null(非NULL)指针指向

任何C可访问的位置。

如果NULL指针本质上不应指向无效的位置,那么为什么我们应该总是使用NULL作为位置0x0,
我们也可以使用其他位置吗?


NULL与位置0无关(除了巧合,大多数机器都是

)。你把空指针的内部表示与整数的内部表示形式混淆了内部表示

0 - 并且实现可以自由地使用一些*其他*表示<空指针的
,例如运行时

系统中某个字节的地址,没有C程序可以合法查看。

是这个必要,每当一个应用程序试图访问由NULL指针指向的位置时,它应该被内核中止
发布一些信号?


不是。这很好,但不是必需的。

我需要你的帮助,因为我越来越困惑了因为我正在阅读关于NULL指针的越来越多的文档。
thanx提前任何帮助.....
Can 0x0 be a valid virtual address in the address space
of an application ?
Yes.
If it is valid, then the location pointed by a NULL pointer
is also valid and application should not receive "SIGSEGV"
( i am talking of unix machine ) while trying to read that
location.
No. If 0 is a valid (C-accessible) address on the machine, then
0 is not a legal representation for a pointer value.
Then how can i distinguish between a NULL pointer and an invalid
location ?
The null (not NULL) pointer tests equal to a integer constant
expression with value 0. Invalid locations don''t.
Is this essential that NULL pointer should not point to any of
the location in the virtual address space ?
It''s essential that the null (not NULL) pointer not point to
any C-accessible location.
If NULL pointer should not essentially point to an invalid
location, then why we should always use NULL to be location 0x0,
can we use some other location as well ?
NULL has nothing to do with location 0 (except by coincidence on
most machines). You''re confusing the internal representation of
the null pointer with the internal representation of the integer
0 - and the implementation is free to use some *other* representaion
of the null pointer, such as the address of some byte in the run-time
system that no C program can legally see.
Is this necessary that whenever an application try to access
location pointed by NULL pointer, it should be aborted by kernel
by posting some signal ?
No. That''s nice, but it''s not required.
Please i need your help, beacuse i am getting more confused
as i am reading more and more documents on NULL pointers.
thanx for any help in advance.....




不要试试并了解实施的内容;试着理解

的规则。


-

Chris" electric hedgehog" Dollin

C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html

C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


文章< 8c ************************** @发布.google.com>,

junky_fellow< ju ********** @ yahoo.co.in>写道:
In article <8c**************************@posting.google.com >,
junky_fellow <ju**********@yahoo.co.in> wrote:
0x0可以是应用程序地址空间中的有效虚拟地址吗?
Can 0x0 be a valid virtual address in the address space
of an application ?




历史上,在几乎所有的C实现中,空指针只是对应于地址0的指针。在这些

实现中,如果你将整数零放入一个

大小与指针相同,然后将该位置视为

指针,你将做的就像你有一个真实的一样

null指针。


C标准不要求事情像这样工作。它们使
使得空指针更抽象,比如Lisp中的nil或Java中的null。

空指针*可以*表示它们对应于

地址0x12345678。在这种情况下,实现必须是小心的,永远不要将真实对象放在地址0x12345678,并且

赋值如int * foo = 0;或int * foo = NULL必须是

编译成代码,将值0x12345678放入foo的位置。

通过某种方式将位置设置为零,例如memset或calloc

不起作用。


所有这些都与取消引用空指针完全分开

会导致某种异常。在具有虚拟内存的现代机器上

通常取消映射包含空指针地址的页面

(实际上是第0页 - 表示null的实现

指针为0x12345678可以取消映射该页面)。总是这样的情况并非总是如此:很多早期的Vax C代码假设空指针

指向一个包含零的字节,因此空指针和空

字符串是可互换的。一旦人们将他们的程序移植到像早期太阳队那样的机器上,这种做法就被消除了,这些机器已经取消了映射。

第0页。


- Richard



Historically, and in almost all C implementations, null pointers are
just pointers corresponding to the address 0. In these
implementations, if you put the integer zero into a location whose
size is the same as a pointer, and then treat that location as a
pointer, you will be doing exactly the same thing as if you had a real
null pointer.

The C standards don''t require things to work like that though. They
make null pointers more abstract, like nil in Lisp or null in Java.
Null pointers *could* be represented so that they corresponded to the
address 0x12345678. In that case, the implementation would have to be
careful never to put a real object at address 0x12345678, and an
assignment like "int *foo = 0" or "int *foo = NULL" would have to be
compile to code that put the value 0x12345678 into foo''s location.
Setting the location to zero by some means such as memset or calloc
would not work.

All this is quite separate from whether dereferencing a null pointer
causes some kind of exception. On modern machines with virtual memory
it''s usual to unmap that page containing the null pointer address
(which in practice is page 0 - an implementation that represented null
pointers as 0x12345678 could unmap that page instead). This wasn''t
always the case: a lot of early Vax C code assumed that null pointers
pointed to a byte containing zero, so that null pointers and empty
strings were interchangable. This practice was eradicated once people
ported their programs to machines like the early Suns, which did unmap
page 0.

-- Richard


" junky_fellow" <菊********** @ yahoo.co.in>在消息中写道

news:8c ************************** @ posting.google.c om ...
"junky_fellow" <ju**********@yahoo.co.in> wrote in message
news:8c**************************@posting.google.c om...
0x0可以是应用程序地址空间中的有效虚拟地址吗?
Can 0x0 be a valid virtual address in the address space
of an application ?




编号零永远不是有效的位于C.


-

Mabden



No. Zero is never a valid location in C.

--
Mabden


这篇关于空指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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