使用指针的问题 [英] Problem with use of pointer

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

问题描述

你好


看看这段小代码:


signed char sca [] = {2,-1, 4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};

signed char sc = *(sca + 1);

短s = *((短*)(sca + sc)+4);


我的问题是:代码是否调用未定义的行为,因为它

尝试转换指向数组外部的指针,或者

那里有一切都很好考虑到

之前解除引用,指针的值在

数组内适当设置?


感谢您的帮助。

braton

推荐答案

br **** @ gmail.com 说:

你好


看看这段小代码:


signed char sca [] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};

签名char sc = *(sca + 1);

short s = *((短*)(sca + sc)+4);


我的问题是:代码是否调用了未定义的行为,因为它是
尝试转换指向数组外部的指针,或者也许

考虑到在

解除引用之前的一切都很好,指针的值在内部适当设置

数组?
Hello

Have a look at this little piece of code:

signed char sca[] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};
signed char sc = *(sca+1);
short s = *((short*)(sca+sc)+4);

My question is: does the code invoke undefined behaviour, because it
tries to convert the pointer which points outside the array, or maybe
there''s everything fine taking into account that just before
dereferencing, the value of pointer is set appropriately inside of
array?



让我们假设sca位于地址ZOG开始:0002


(ZOG部分只是提醒我们,指针不一定是完全的数字值。)


sca + 1是ZOG:0003,这很好。


sc = *(sca + 1);也很好,并将-1加载到sc。


(sca + sc)是ZOG:0001,它指向对象外部,并且

立即意味着未定义的行为。因此,无法保证ZOG:0001是短整数的正确对齐的
地址,因此强制转换为*,

。如果sizeof(short)足够大,那么向它添加4 /还/调用undefined

行为,并且将所得到的

指针去掉,只是将未定义的行为堆积成一个大的摇摆不定的行为桩。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Let''s start off by assuming that sca is at address ZOG:0002

(The ZOG part is just to remind us that pointers need not be wholly
numeric values.)

sca + 1 is ZOG:0003, which is fine.

sc = *(sca + 1); is fine, too, and loads -1 into sc.

(sca + sc) is ZOG:0001, which points outside the object, and that
immediately means undefined behaviour. So does the cast to short *,
because there is no guarantee that ZOG:0001 is a correctly-aligned
address for a short int. Adding 4 to it /also/ invokes undefined
behaviour if sizeof(short) is big enough, and dereffing the resulting
pointer is just heaping undefined behaviour up into a big wobbly pile.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Hello
Hello

>
>

看看这段代码:
Have a look at this little piece of code:


signed char sca [ ] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};

signed char sc = *(sca + 1);

短s = *((短*)(sca + sc)+4);
signed char sca[] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};
signed char sc = *(sca+1);
short s = *((short*)(sca+sc)+4);


我的问题是:代码是否调用了未定义的行为,因为它b / b
尝试转换指向外部的指针数组,或者也许

有一切都很好考虑到在

解除引用之前,指针的值在内部适当设置

阵?
My question is: does the code invoke undefined behaviour, because it
tries to convert the pointer which points outside the array, or maybe
there''s everything fine taking into account that just before
dereferencing, the value of pointer is set appropriately inside of
array?



让我们假设sca位于地址ZOG开始:0002


(ZOG部分只是提醒我们,指针不一定是完全的数字值。)


sca + 1是ZOG:0003,这很好。


sc = *(sca + 1);也很好,并将-1加载到sc。


(sca + sc)是ZOG:0001,它指向对象外部,并且

立即意味着未定义的行为。因此,无法保证ZOG:0001是短整数的正确对齐的
地址,因此强制转换为*,

。如果sizeof(short)足够大,那么向它添加4 /还/调用undefined

行为,并且将所得到的

指针去掉,只是将未定义的行为堆积成一个大的摇摆不定的行为堆。


Let''s start off by assuming that sca is at address ZOG:0002

(The ZOG part is just to remind us that pointers need not be wholly
numeric values.)

sca + 1 is ZOG:0003, which is fine.

sc = *(sca + 1); is fine, too, and loads -1 into sc.

(sca + sc) is ZOG:0001, which points outside the object, and that
immediately means undefined behaviour. So does the cast to short *,
because there is no guarantee that ZOG:0001 is a correctly-aligned
address for a short int. Adding 4 to it /also/ invokes undefined
behaviour if sizeof(short) is big enough, and dereffing the resulting
pointer is just heaping undefined behaviour up into a big wobbly pile.



我忘了提到sizeof(短)== 2所以如果加上4将是

可能那么指针会指向阵列中有1个,但我看到它不会达到
到达那么远。

感谢您的澄清!

braton

I forgot mention that sizeof(short)==2 so if addition of 4 would be
possible then pointer would points to 1 in array, but I see it won''t
reach that far.
Thank you for clarification!
braton


br****@gmail.com 写道:
br****@gmail.com wrote:

>>> signed char sca [] = {2,-1,4, 3,5,3,-8,1,9,2,3,6,-1,0,12,2};
签名字符sc = *(sca + 1);
短片= *((短*)(sca + sc)+4);
>>>signed char sca[] = {2,-1,4,3,5,3,-8,1,9,2,3,6,-1,0,12,2};
signed char sc = *(sca+1);
short s = *((short*)(sca+sc)+4);


>>>我的问题是:代码是否调用未定义的行为,
>>>My question is: does the code invoke undefined behaviour,


( sca + sc)是ZOG:0001,它指向对象外部,并且
立即意味着未定义的行为。因此,无法保证ZOG:0001是短整数的正确对齐的地址,因此转换为短*,
如果sizeof(short)足够大,则将4添加到/ / /调用undefined
行为,并且对结果的指针进行去除只是将未定义的行为堆积到一个大的摇摆堆中。


(sca + sc) is ZOG:0001, which points outside the object, and that
immediately means undefined behaviour. So does the cast to short *,
because there is no guarantee that ZOG:0001 is a correctly-aligned
address for a short int. Adding 4 to it /also/ invokes undefined
behaviour if sizeof(short) is big enough, and dereffing the resulting
pointer is just heaping undefined behaviour up into a big wobbly pile.



我忘了提到sizeof(短)== 2所以如果加上4将是

可能那么指针将指向1 in阵列,但我看到它不会达到目标。


I forgot mention that sizeof(short)==2 so if addition of 4 would be
possible then pointer would points to 1 in array, but I see it won''t
reach that far.



请记住,如果您询问是否在此新闻组中定义了行为,

假定的上下文是由C标准定义的,没有

指定sizeof(短)。如果你想知道代码的行为是否是为特定实现定义的,那么你需要在某个地方询问

来解决这个实现。
< br $> b $ b -

Thad

Keep in mind that if you ask if behavior is defined in this newsgroup,
the assumed context is definition by the C Standard, which doesn''t
specify sizeof(short). If you want to know if the behavior of your code
is defined for a particular implementation, then you need to ask
somewhere that addresses that implementation.

--
Thad


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

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