取消引用数组指针... UB? [英] Dereference an array pointer... UB?

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

问题描述



您是否认为我们可以就标准未定义代码行为的

达成任何形式的共识?


int my_array [5];


int const * const pend = *(& my_array + 1);


考虑到语言的语法,那么我们肯定会使用无效指针取消引用...但是如果我们考虑

语言的机制,那么我们就知道什么都没有"发生"当我们将一个指针取消引用到数组时,因为数组是按指针处理的。


-
$ b $bTomás óhéilidhe


Do you think we can reach any kind of consensus on whether the
following code''s behaviour is undefined by the Standard?

int my_array[5];

int const *const pend = *(&my_array + 1);

Considering the syntax of the language, then we definitely do
dereference an invalid pointer... but if we consider the mechanics of the
language, then we know that nothing "happens" when we dereference a pointer
to an array, because arrays are dealt with in terms of pointers.

--
Tomás ó héilidhe

推荐答案



"Tomásóhéilidhe" < to *@lavabit.com写了留言

"Tomás ó héilidhe" <to*@lavabit.comwrote in message

>

你认为我们能否达成任何形式的共识<标准未定义代码的行为后面的



int my_array [5];


int const * const pend = *(& my_array + 1);


考虑到语言的语法,我们肯定会将无效指针取消引用。 ..但如果我们考虑

语言的机制,那么我们就知道什么都没有发生。当我们取消引用一个指向数组的
指针时,因为数组是按指针处理的。
>
Do you think we can reach any kind of consensus on whether the
following code''s behaviour is undefined by the Standard?

int my_array[5];

int const *const pend = *(&my_array + 1);

Considering the syntax of the language, then we definitely do
dereference an invalid pointer... but if we consider the mechanics of the
language, then we know that nothing "happens" when we dereference a
pointer to an array, because arrays are dealt with in terms of pointers.



my_array和& my_array解析为同一件事情。这是

语言的怪癖。


-

免费游戏和编程好东西。
< a rel =nofollowhref =http://www.personal.leeds.ac.uk/~bgy1mmtarget =_ blank> http://www.personal.leeds.ac.uk/~bgy1mm

my_array and &my_array resolve to the same thing. It''s a quirk of the
language.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


" Malcolm McLean" < re ******* @ btinternet.comwrites:
"Malcolm McLean" <re*******@btinternet.comwrites:

" Tom ?? s ?? H 18 ilidhe" < to *@lavabit.com写了留言
"Tom??s ?? h??ilidhe" <to*@lavabit.comwrote in message

>>
你认为我们能否达成任何形式的共识
以下代码的行为未被标准定义?

int my_array [5];

int const * const pend = *(& my_array + 1);

考虑到语言的语法,那么我们肯定会取消引用无效指针......但是如果我们考虑
语言的机制,那么我们就知道什么都没有;发生"当我们取消引用指向数组的指针时,因为数组是按指针处理的。
>>
Do you think we can reach any kind of consensus on whether the
following code''s behaviour is undefined by the Standard?

int my_array[5];

int const *const pend = *(&my_array + 1);

Considering the syntax of the language, then we definitely do
dereference an invalid pointer... but if we consider the mechanics of the
language, then we know that nothing "happens" when we dereference a
pointer to an array, because arrays are dealt with in terms of pointers.



my_array和& my_array解析为同一个东西。这是

语言的怪癖。

my_array and &my_array resolve to the same thing. It''s a quirk of the
language.



但my_array + 1和& my_array + 1不要。 解决一词允许你

是正确的(因为你可以表达你喜欢的东西)但它隐藏了两个表达式之间的重要区别 - 它们的类型。


-

Ben。

But my_array + 1 and &my_array + 1 don''t. The word "resolve" allows you
to be right (since you can mean what you like by it) but it hides the
important difference between the two expressions -- their type.

--
Ben.


2月11日晚上8:36,Malcolm McLean < regniz ... @ btinternet.comwrote:
On Feb 11, 8:36 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:

"Tomásóhéilidhe" < t ... @ lavabit.comwrote in message
"Tomás ó héilidhe" <t...@lavabit.comwrote in message

您认为我们可以就

以下是否达成任何形式的共识标准未定义代码的行为?
Do you think we can reach any kind of consensus on whether the
following code''s behaviour is undefined by the Standard?


int my_array [5];
int my_array[5];


int const * const pend = *(& my_array + 1);
int const *const pend = *(&my_array + 1);


考虑到语言的语法,那么我们肯定会将无效指针取消引用...但是如果我们考虑的话

语言的机制,然后我们知道什么都没有发生。当我们取消引用一个指向数组的指针时,因为数组是按指针处理的。
Considering the syntax of the language, then we definitely do
dereference an invalid pointer... but if we consider the mechanics of the
language, then we know that nothing "happens" when we dereference a
pointer to an array, because arrays are dealt with in terms of pointers.



my_array和& my_array解决同样的问题。这是

语言的怪癖。


my_array and &my_array resolve to the same thing. It''s a quirk of the
language.



只在价值背景下。

我相信它是未定义的行为。

你取消引用一个指针超过一个物体的结尾。

它与

基本相同 -

int * foo;

int * bar = *(& foo + 1);

-

哪个无效。

& foo是一个对象,可以将其视为一个包含1个元素的数组。

因此,& foo + 1是一个有效的指针,无法解除引用,

但你取消引用它。


这是无效的。


但是,我不是100%肯定这一点,但似乎是合乎逻辑的

并且正确。

Only in value context.
I believe it''s undefined behavior.
You dereference a pointer past the end of an object.
It is essentially the same with
--
int *foo;
int *bar = *(&foo+1);
--
Which is invalid.
&foo is an object, which can be treated as an array with 1 element.
Therefore, &foo+1 is a valid pointer, which cannot be dereferenced,
however you do dereference it.

It is invalid.

I am, however, not 100% sure about this, but it appears to be logical
and correct.


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

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