关于阵列访问环绕标准的标准是什么? [英] What does the standard say about array access wraparound?

查看:64
本文介绍了关于阵列访问环绕标准的标准是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这样:


int i,sum;

int * array;

for(sum = 0,i = 0; i< len; i ++){

sum + = array [i];

}


转换为这(暂时不介意为什么):


int i,sum;

int * array;

int * arrl ;

arl =& array [-len];

for(sum = 0,i = len; i< 2 * len; i ++){

sum + = arrl [i];

}


它应该给出相同的结果。但是有一些有趣的事情会发生。例如,如果& array是1000并且

len是100000.在这种情况下,arrl将保存一个地址

(1000-100000),这可能是因为

指针应该是unsigned int(无论int是什么大小)。

它指向的地址为MAX_POINTER - 100000 + 1000.

第二种形式循环循环开始i = len(100000)所以

arrl [100000]将回绕并指向与数组[0]相同的

位置。 br />

或者它会吗?


这种类型的数组访问似乎可能会在

之上存储器"可能会引发错误。


C标准对此有何评价(如果有的话)?


谢谢,


David Mathog
ma****@caltech.edu

Caltech生物学部门序列分析设施经理

If this:

int i,sum;
int *array;
for(sum=0, i=0; i<len; i++){
sum += array[i];
}

is converted to this (never mind why for the moment):

int i,sum;
int *array;
int *arrl;
arl=&array[-len];
for(sum=0,i=len; i<2*len; i++){
sum += arrl[i];
}

it should give the same result. But there are some funny
things that can happen. For instance, if &array is 1000 and
len is 100000. In that case arrl will hold an address
(1000-100000) which presumably wraps around since the
pointer should be an unsigned int (whatever size int is).
The address it points to will be MAX_POINTER - 100000 + 1000.
When the second form loop loop begins i=len (100000) so
arrl[100000] will wrap back around and point to the same
place as array[0].

Or will it?

It seems possible that this sort of array access "off the top of
memory" could trigger a fault.

What does the C standard say about this (if anything)?

Thanks,

David Mathog
ma****@caltech.edu
Manager, Sequence Analysis Facility, Biology Division, Caltech

推荐答案



" David Mathog" <毫安**** @ caltech.edu>在消息中写道

news:20040527080256.4fbb14a0.ma **** @ caltech.edu ...

"David Mathog" <ma****@caltech.edu> wrote in message
news:20040527080256.4fbb14a0.ma****@caltech.edu...
如果这样:

int i, sum;
int * array;


请注意,您命名为''array''的对象是

*而不是*数组,它是一个指针。还要注意

你没有给这个指针一个有效的值,

所以评估它会产生不确定的行为。

for(sum = 0,i = 0; i< len; i ++){


你还没有定义''len''。

另请注意,如果你想用它们来索引一个数组的元数据,那么对象''我'

和''len''的类型应该是''size_t'',而不是''int''。

sum + = array [i];


未定义的行为。

}

转换为此(暂时不介意为什么):

int i,sum;
int * array;
int * arrl;


两个指针。没有阵列。

arl =& array [-len];


未定义的行为。即使名为''array''

的指针有一个有效值,行为仍然是未定义的。

唯一有效的索引是那些在
中引用的索引
同一个对象。

for(sum = 0,i = len; i< 2 * len; i ++){
sum + = arrl [i];
}
它应该给出相同的结果。


嗯,是的,undefined == undefined。

但是有一些有趣的事情可能会发生。


这基本上是''未定义的行为''。

可能发生的另一个有趣的事情是

您的键盘可以发出100,000伏特。

例如,如果&数组是1000并且
len是100000.在这种情况下,arrl将保留一个地址
( 1000-100000)大概包裹在


在C中没有包裹数组的东西。


指针应该是unsigned int(无论int是什么大小)。


指针是*不是整数。这是一个指针。它的
表示和结构是特定于实现的。


它指向的地址是MAX_POINTER


那里在C中没有MAX_POINTER这样的东西。

- 100000 + 1000.
当第二个循环循环开始时i = len(100000)所以
arrl [100000]将回绕并指向与数组[0]相同的位置。

或者它会吗?


是的。不,也许吧。有时。决不。仅在周三

在东京下雨时。你的代码会产生不确定的行为。

这种类型的数组似乎可以在内存的顶部进行访问。可能会引发错误。


错误在于您的代码。

C标准对此有何看法(如果有的话)?
If this:

int i,sum;
int *array;
Note that the object you''ve named ''array'' is
*not* an array, it''s a pointer. Also note that
you''ve not given this pointer a valid value,
so evaluating it will produce undefined behavior.
for(sum=0, i=0; i<len; i++){
You haven''t defined ''len''.
Also note that if you want to use them to index
(or record the size of) an array, the objects ''i''
and ''len'' should be of type ''size_t'', not ''int''.
sum += array[i];
Undefined behavior.
}

is converted to this (never mind why for the moment):

int i,sum;
int *array;
int *arrl;
Two pointers. No arrays.
arl=&array[-len];
Undefined behavior. Even if the pointer named ''array''
had a valid value, the behavior is still undefined.
The only valid indices are those which refer within
the same object.
for(sum=0,i=len; i<2*len; i++){
sum += arrl[i];
}

it should give the same result.
Well, yes, undefined == undefined.
But there are some funny
things that can happen.
That''s essentially what ''undefined behavior'' is.
Another ''funny thing'' that might happen is that
your keyboard could emit 100,000 volts.
For instance, if &array is 1000 and
len is 100000. In that case arrl will hold an address
(1000-100000) which presumably wraps around
There is no such thing as ''wrapping around'' of arrays in C.
since the
pointer should be an unsigned int (whatever size int is).
A pointer is *not* an integer. It''s a pointer. Its
representation and structure is implementation-specific.

The address it points to will be MAX_POINTER
There''s no such thing as ''MAX_POINTER'' in C.
- 100000 + 1000.
When the second form loop loop begins i=len (100000) so
arrl[100000] will wrap back around and point to the same
place as array[0].

Or will it?
Yes. No. Maybe. Sometimes. Never. Only on Wednesdays
when it rains in Tokyo. Your code produces undefined behavior.

It seems possible that this sort of array access "off the top of
memory" could trigger a fault.
The fault is with your code.

What does the C standard say about this (if anything)?




它说您的代码无效C.


您正在阅读哪些C书?


- Mike



It says your code is not valid C.

Which C book(s) are you reading?

-Mike


Mike Wahler写道:
Mike Wahler wrote:
" David Mathog" <毫安**** @ caltech.edu>在消息中写道
新闻:20040527080256.4fbb14a0.ma **** @ caltech.edu ...
"David Mathog" <ma****@caltech.edu> wrote in message
news:20040527080256.4fbb14a0.ma****@caltech.edu...
for(sum = 0,i = 0; i< len; i ++){
for(sum=0, i=0; i<len; i++){



还要注意,如果你想用它们来索引(或记录大小)一个数组,对象''我'和/''' len''的类型应为''size_t'',而不是''int''。



Also note that if you want to use them to index
(or record the size of) an array, the objects ''i''
and ''len'' should be of type ''size_t'', not ''int''.

sum + = array [i];
sum += array[i];




嗯,为什么用作数组索引的变量是

类型''size_t''? K& R-2nd / ANSI使用''int''经常

来索引数组。


案例



Huh, why should a variable used as array index be of
type ''size_t''? K&R-2nd/ANSI uses ''int'' quite often
to index arrays.

Case


In< 20 **************************** @ caltech.edu> David Mathog< ma **** @ caltech.edu>写道:
In <20****************************@caltech.edu> David Mathog <ma****@caltech.edu> writes:
如果这样:
int i,sum;
int * array;
for(sum = 0,i = 0; i< len; i ++){
sum + = array [i];
}
转换为此(暂时不介意为什么):

int i,sum;
int * array;
int * arrl;
arl =& array [-len];
for(sum = 0, i = len; i< 2 * len; i ++){
sum + = arrl [i];
}
它应该给出相同的结果。
If this:

int i,sum;
int *array;
for(sum=0, i=0; i<len; i++){
sum += array[i];
}

is converted to this (never mind why for the moment):

int i,sum;
int *array;
int *arrl;
arl=&array[-len];
for(sum=0,i=len; i<2*len; i++){
sum += arrl[i];
}

it should give the same result.




帮自己一个忙,阅读常见问题解答。不要回来,直到你已经完成它了!


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da ** ***@ifh.de


这篇关于关于阵列访问环绕标准的标准是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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