数组没有限制? [英] No limits in arrays??

查看:74
本文介绍了数组没有限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C中是否没有ArrayOutOfBounds异常?


我刚刚做了:


int main()

{

int a [4]; // a是一个包含4个整数的数组。

a [5] = 999; //认为这会出错!

printf(a [5]:%d \ n,a [5]);

返回1;

}


当我运行这个程序时


a [5]:999

被打印。


当我只指定

a应该包含4个整数时,如何在[5]中放入一些东西?

Is there no such a thing as an ArrayOutOfBounds exception in C?

I just did:

int main()
{
int a[4]; // a is an array that contains 4 integer.
a[5] = 999; // Thought this would give an error!
printf("a[5]: %d\n", a[5]);
return 1;
}

When I run this program

a[5]: 999

gets printed.

How is it possible to put something in a[5] when I have only specified that
a should contain 4 integers?

推荐答案

C中没有这样的结果。结果未定义。对于你的编译器和
运行时环境,该程序恰好正常工作。

No such thing in C. The results are undefined. For your compiler and
runtime environment, the program just happened to work.


Paminu< ja ***** *@asd.com>写道:
Paminu <ja******@asd.com> writes:
C中是否没有像ArrayOutOfBounds这样的异常?

我刚刚做了:

int main()
{
int a [4]; // a是一个包含4个整数的数组。
a [5] = 999; //认为这会产生错误!
printf(a [5]:%d \ n,a [5]);
返回1;
}
当我运行这个程序时

a [5]:999

被打印出来。

怎么可能放东西在[5]中我只指定
a应该包含4个整数?
Is there no such a thing as an ArrayOutOfBounds exception in C?

I just did:

int main()
{
int a[4]; // a is an array that contains 4 integer.
a[5] = 999; // Thought this would give an error!
printf("a[5]: %d\n", a[5]);
return 1;
}

When I run this program

a[5]: 999

gets printed.

How is it possible to put something in a[5] when I have only specified that
a should contain 4 integers?




这个刚出现。

访问超出数组边界的数组元素调用

未定义的行为。这并不意味着编译器会检测到

错误;它特别意味着它不需要这样做。未定义的

行为可以做任何事情;标准的笑话是,它可以合法地让恶魔飞出你的鼻子。


在这种情况下,你可能只是踩着记忆过去的位置

数组的结尾。如果实现没有使用该位置的任何东西,那很可能不会产生一个

可见错误(不幸的是)。如果它确实用它来做某事,你可能会破坏函数的返回地址或其他任何东西。


避免未定义行为的责任完全是您的。

将C视为没有安全功能的电动工具;如果使用得当,它有效

,但在使用不当时会很危险。


注意,访问[4]也会调用未定义的行为;

有效元素是0,1,2和3.


-

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

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

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



This just came up recently.

Accessing an array element beyond the bounds of the array invokes
undefined behavior. This doesn''t mean the compiler will detect the
error; it specifically means it''s not required to do so. Undefined
behavior can do literally anything; the standard joke is that it can
legally make demons fly out of your nose.

In this case, you''re probably just stepping on a memory location past
the end of the array. If the implementation doesn''t happen to be
using ithat location for anything, it will most likely not create a
visible error (unfortunately). If it does use it for something, you
might clobber the function''s return address or anything else.

The responsibility for avoiding undefined behavior is entirely yours.
Think of C as a power tool without safety features; it''s effective
when used properly, but dangerous when used improperly.

Note that accessing a[4] would also invoke undefined behavior; the
valid elements are 0, 1, 2, and 3.

--
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.


An *** *******@gmail.com 写道:
C中没有这样的东西。结果是未定义的。对于您的编译器和运行时环境,该程序恰好正常工作。
No such thing in C. The results are undefined. For your compiler and
runtime environment, the program just happened to work.




我们再来一次。


如果您想通过groups.google.com发布后续内容,请不要使用

损坏的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。


和*请*向Google报告他们破坏的界面。搜索

以了解此新闻组中上段的出现情况;那将是
让你知道它有多大问题。


-

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

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

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



Here we go again.

If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

And *please* complain to Google about their broken interface. Search
for occurrences of the above paragraph in this newsgroup; that will
give you an idea of how big a problem it is.

--
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.


这篇关于数组没有限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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