关于数组超出索引的问题 [英] question about array out of bound index
问题描述
hai
我需要知道数组出界错误,我们的讲师认为这是错误的。我认为当你粗暴地指出了
大小它会显示错误,但是当我在我的电脑上工作时它
只显示声明后分配的值?
hai
I need to know about array out of bound error which is thought by
our lecturer .she thought that when u gross the intialially specified
size it will show that error,but when I am working on my computer it
shows only the value which is allocated after the declaration?
推荐答案
venkatesh写道:
venkatesh wrote:
hai
我需要知道数组出界错误,这是由我们的讲师认为的。她认为当你粗略地指出那个大小会显示错误时,但是当我在我的电脑上工作时它只显示声明后分配的值吗?
hai
I need to know about array out of bound error which is thought by
our lecturer .she thought that when u gross the intialially specified
size it will show that error,but when I am working on my computer it
shows only the value which is allocated after the declaration?
标准C没有用于对数组进行边界检查的工具;当你
超过数组的界限时,你调用可怕的UB - undefined
行为 - 此时*任何*都可能发生。如果你很幸运,你的
程序会以一种易于诊断的方式崩溃。如果你不是......好吧,
提防鼻子恶魔!
(请参阅常见问题解答。)
>
HTH,
- g
-
Artie Gold - 德克萨斯州奥斯汀> http://goldsays.blogspot.com
"你不能亲吻*,除非你错过**
[* - 保持简单,愚蠢。 ** - 简单,愚蠢。]
Standard C has no facilities for bounds checking on arrays; when you
exceed the bounds of an array you invoke the dreaded UB -- undefined
behavior -- at which point *anything* can happen. If you''re lucky your
program crashes in a way that''s easy to diagnose. If you''re not...well,
beware of nasal demons!
(Please see the FAQ.)
HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can''t KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
venkatesh写道:
venkatesh wrote:
hai
我需要知道数组出界我们的讲师认为错误。她认为当你粗略地指出大小时它会显示错误,但是当我在我的电脑上工作时它只显示其中的值声明后分配?
hai
I need to know about array out of bound error which is thought by
our lecturer .she thought that when u gross the intialially specified
size it will show that error,but when I am working on my computer it
shows only the value which is allocated after the declaration?
我真的不确定你在说什么。
int aarray [10];与
int * parray = malloc(10);
sizeof(aarray)应该与sizeof(parray)相同
-
Andrew Poelstra< http://www.wpsoftware.net/blog>
系列中的每个素数作为笑话
当我拿到最后的帖子时,所有模式都清晰了
I''m really not sure what you''re saying.
int aarray[10]; is the same as
int *parray = malloc(10);
sizeof (aarray) should be the same as sizeof (parray)
--
Andrew Poelstra <http://www.wpsoftware.net/blog>
Every prime number in a series as a joke
Made all the patterns clear when I took that final toke
venkatesh写道:
venkatesh wrote:
hai
我需要知道我们的讲师认为的数组出界错误。她认为当你粗略地指出大小时会显示错误,但是当我在计算机上工作时它只显示声明后分配的值?
hai
I need to know about array out of bound error which is thought by
our lecturer .she thought that when u gross the intialially specified
size it will show that error,but when I am working on my computer it
shows only the value which is allocated after the declaration?
不允许读取数组末尾,但标准确实
不要求检测或报告此内容。标准确实
保证将一个指针递增到指向一个结尾的一个指针,而b / b $ b $数组不会溢出。进一步导致未定义的行为。
无论哪种方式,你仍然不允许引用这样的指针
(也就是UB)。 />
< OT>
某些C实现提供所谓的边界检查作为
扩展名,通常采用编译时选项的形式。打开这个
往往会产生更大更慢的代码,因为包含额外的代码
来检查对数组的每个索引访问。
< ; / OT>
Reading past the end of an array is not allowed, but the Standard does
not require this to be detected or reported. The Standard does
guarantee that incrementing a pointer to point just one past the end of
an array will not overflow. Going further leads to Undefined Behaviour.
Either way, you''re still not allowed to dreference such a pointer
(that''s UB as well).
<OT>
Some C implementations offer so called "bounds checking" as an
extension, usually in the form of a compile time option. Turning this
on tends to produce larger and slower code, as extra code is included
to check every indexed access to the array.
</OT>
这篇关于关于数组超出索引的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!