涉及NULL指针的指针算法 [英] Pointer arithmetic involving NULL pointers

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

问题描述

是否向指向非空的指针添加0等于NULL合法?


int * p = NULL;

p + = 0;


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

解决方案

Christopher Benson-Manica写道:

将0添加到指向非空的指针等于NULL合法吗?

int * p = NULL;
p + = 0;




从未考虑过,但我认为它是未定义的。


6.5.6附加运算符

/ 8 / [...]如果指针操作数和结果都是

指向同一个数组对象的元素,或者一个过去

数组对象的最后一个元素,评估

不会产生溢出;否则,行为

未定义。 [...]


由于上面的'p''没有指向任何数组

对象的任何元素,所以否则抱着。


如果你不介意我的问题,你还想做什么?

我已经将零添加到指针值很多时间,如


char * p = string_with_possible_leading_spaces;

p + = strspn(p," \\\\\\\\\\ r"


....但我不记得被诱惑做任何这样的事情

有一个NULL值。什么是' up?


-
Er ****** ***@sun.com


Christopher Benson-Manica< at *** @ nospam.cyberspace.org>写道:< blockquote class =post_quotes>将0添加到指向非空的等于NULL合法的指针?

int * p = NULL;
p + = 0;




不,它会调用未定义的行为.C99 6.5.6p4说:


当一个具有整数类型的表达式被添加或减去

来自指针,结果具有指针operan的类型d。

[...]

如果指针操作数和结果都指向

的元素相同的数组对象,或者一个过了数组的最后一个元素

对象,评估不会产生溢出;否则,

行为未定义。


添加0没有声明的例外.C90标准有

类似或相同的措辞。


在大多数实现中,操作不会导致陷阱,并且

结果将等于NULL - 当然是未定义行为的有效后果。


-

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

San迭戈超级计算机中心< *> < http://users.sdsc.edu/~kst>

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


在文章< ch ********** @ chessie.cirr.com>,

Christopher Benson-Manica< at *** @ nospam.cyberspace.org>写道:

是否向指向非空的指针添加0等于NULL合法?

int * p = NULL;
p + = 0;




否;指针添加仅定义为指向有效

对象的指针或一个,因此未定义向空指针添加任何内容(甚至0)。

(N869 6.5.6) #8,另见#7。)


(但今天sigmonster做出了不错的选择。)

dave


-

Dave Vandervies dj******@csclub.uwaterloo.ca

我会向编译器供应商提出严重的QOI投诉,如果其中任何一个

这些重新格式化我的鼻子或导致Scott Nudds飞出我的硬盘。

- comp.lang.c中的Jack Klein


Is adding 0 to a pointer to non-void that is equal to NULL legal?

int *p=NULL;
p+=0;

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

解决方案

Christopher Benson-Manica wrote:

Is adding 0 to a pointer to non-void that is equal to NULL legal?

int *p=NULL;
p+=0;



Never considered it before, but I think it''s undefined.

6.5.6 Additive operators
/8/ [...] If both the pointer operand and the result
point to elements of the same array object, or one past
the last element of the array object, the evaluation
shall not produce an overflow; otherwise, the behavior
is undefined. [...]

Since `p'' above does not point to any element of any array
object, the "otherwise" holds.

If you don''t mind my asking, what are you trying to do?
I''ve added zero to a pointer value plenty of times, as in

char *p = string_with_possible_leading_spaces;
p += strspn(p, " \t\f\n\r";

.... but I can''t recall being tempted to do any such thing
with a NULL value. What''s up?

--
Er*********@sun.com


Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:

Is adding 0 to a pointer to non-void that is equal to NULL legal?

int *p=NULL;
p+=0;



No, it invokes undefined behavior. C99 6.5.6p4 says:

When an expression that has integer type is added to or subtracted
from a pointer, the result has the type of the pointer operand.
[...]
If both the pointer operand and the result point to elements of
the same array object, or one past the last element of the array
object, the evaluation shall not produce an overflow; otherwise,
the behavior is undefined.

There''s no stated exception for adding 0. The C90 standard has
similar or identical wording.

On most implementations, the operation won''t cause a trap, and the
result will compare equal to NULL -- which is, of course, a valid
consequence of undefined behavior.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


In article <ch**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:

Is adding 0 to a pointer to non-void that is equal to NULL legal?

int *p=NULL;
p+=0;



No; pointer addition is only defined for pointers to or one past a valid
object, so adding anything (even 0) to a null pointer is undefined.
(N869 6.5.6#8, see also #7.)

(But the sigmonster made a good choice today.)
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I would lodge a serious QOI complaint with the compiler vendor if either of
these reformatted my nose or caused Scott Nudds to fly out of my hard drive.
--Jack Klein in comp.lang.c


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

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