取消引用NULL [英] De-referencing NULL

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

问题描述

#include< stdio.h>


int

main(void){

char * p = NULL ;

printf("%c\ n",* p);

返回0;

}


此代码段打印0(在Win XP上使用DJGPP编译)。 Visual C ++ 6.0

编译带有诊断程序的程序,程序在

运行时崩溃。 Linux上的gcc给出了SEGMENTATION FAULT。


这看起来像DJGPP的一个错误。 NULL可以有不同的

表示但是编译器如何允许在没有任何编译时或运行时错误的情况下取消引用NULL




PS:我不是在讨论DJGPP。我只是想知道这个行为是否可以用C规格来确定这个行为?

#include <stdio.h>

int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?

P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?

推荐答案

> #include< stdio .H>
>#include <stdio.h>

>
int
main(void){

char * p = NULL;

printf("%c\ n",* p);

返回0;
}

此片段打印0(使用Win XP上的DJGPP)。 Visual C ++ 6.0
使用诊断程序编译程序,当程序运行时程序崩溃。 Linux上的gcc给出了SEGMENTATION FAULT。

这看起来像是DJGPP的一个bug。
>
int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP.



无论实现在这里做什么或不做什么,它都不是每个C标准的错误

。您可能会认为该程序无法删除自己的源代码是一个实施质量问题。

Whatever the implementation does or doesn''t do here, it''s not a bug
per the C standard. You may argue that it is a quality-of-implementation
issue that the program fails to delete its own source code.


> NULL可以有不同的表示但是编译器如何允许在没有任何编译时或运行时错误的情况下取消引用NULL

>NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?



在编译时或运行时,不需要C编译器来诊断
的取消引用。未定义的行为可以包括

取零或忽略写入尝试,或者它可以包括获取
零或忽略写入尝试,除非当老板或客户

正在观看演示期间,然后将他的领带着火。

A C compiler is not required to diagnose a de-reference of NULL at
either compile time or run time. Undefined behaviour can include
fetching zero or ignoring write attempts, or it can include fetching
zero or ignoring write attempts except when the boss or customer
is watching during a demonstration, then catching his tie on fire.


> PS:我不是在讨论DJGPP。我只是想知道这个行为是否适用于C规范?
>P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?



无论在这种情况下发生什么,C规格都可以。

禁止任何可能的行为,即使它违反了

物理法则。

No matter what happens in this situation, it''s OK with the C specs.
No possible behavior is forbidden, even if it violates the laws of
physics.


rahul写道:
rahul wrote:

#include< stdio.h>


int

main(void){

char * p = NULL ;

printf("%c\ n",* p);

返回0;

}


此代码段打印0(在Win XP上使用DJGPP编译)。 Visual C ++ 6.0

编译带有诊断程序的程序,程序在

运行时崩溃。 Linux上的gcc给出了SEGMENTATION FAULT。


这看起来像DJGPP的一个错误。 NULL可以有不同的

表示但是编译器如何允许在没有任何编译时或运行时错误的情况下取消引用NULL




PS:我不是在讨论DJGPP。我只是想知道这个行为是否可以使用C规格获得

#include <stdio.h>

int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?

P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?



行为定义为未定义。这是相关的一点来自

n1256.pdf:


6.5.3.2


4一元*算子表示间接。如果操作数指向

函数,则结果是函数指示符;如果它指向一个

对象,则结果是指定对象的左值。如果操作数

有类型??指针类型??,则结果类型为?? ?? ??。如果为指针分配了

无效值,则

一元*运算符的行为是未定义的.87)


指定脚注的一部分(不是规范性的):


由一元*取消引用指针的无效值*

运算符是空指针,[...]


由于没有违反约束,实施没有义务

发出诊断,但有一个是有用的。但是在所有可能的情况下很难做到这一点



The behaviour is defined to be undefined. Here is the relevant bit from
n1256.pdf:

6.5.3.2

4 The unary * operator denotes indirection. If the operand points to a
function, the result is a function designator; if it points to an
object, the result is an lvalue designating the object. If the operand
has type ??pointer to type??, the result has type ??type??. If an
invalid value has been assigned to the pointer, the behavior of the
unary * operator is undefined.87)

Part of the indicated footnote (which is not normative):

Among the invalid values for dereferencing a pointer by the unary *
operator are a null pointer, [ ... ]

Since no constraint is violated, the implementation is not obliged to
emit a diagnostic, though one would be useful. But it''s hard to do this
in all possible cases.


rahul< ra ***** **** @ gmail.com写:
rahul <ra*********@gmail.comwrites:

#include< stdio.h>


int

main(无效){

char * p = NULL;

printf("%c\ n",* p);

返回0;

}


此代码段打印0(在Win XP上使用DJGPP编译)。 Visual C ++ 6.0

编译带有诊断程序的程序,程序在

运行时崩溃。 Linux上的gcc给出了SEGMENTATION FAULT。


这看起来像DJGPP的一个错误。 NULL可以有不同的

表示但是编译器如何允许在没有任何编译时或运行时错误的情况下取消引用NULL




PS:我不是在讨论DJGPP。我只是想知道这个行为是否可以使用C规格获得

#include <stdio.h>

int
main (void) {
char *p = NULL;
printf ("%c\n", *p);
return 0;
}

This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
compiles the program with diagnostics and the program crashes when
ran. gcc on Linux gives SEGMENTATION FAULT.

This looks like a bug with DJGPP. NULL can have different
representations but how come the compiler allows de-referencing NULL
without any compile time or run-time errors?

P.S : I am not discussing DJGPP. I just want to know if this behaviour
is OK with C specs?



行为未定义,这意味着任何行为都可以,因为标准涉及到

。如果你的实现没有产生

诊断但是让恶魔飞出你的鼻子,它可能会违反物理定律,但它不会违反标准。 br />

-

Keith Thompson(The_Other_Keith) ks *** @ mib。组织< http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

The behavior is undefined, which means that any behavior is ok as far
as the standard is concerned. If your implementation produced no
diagnostics but made demons fly out of your nose, it might violate the
laws of physics, but it wouldn''t violate the standard.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


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

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