访问其边界外的数组 [英] accessing an array outside its bounds

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

问题描述

你好


这似乎在我的系统中有效,直到变量''我'达到15985,然后

程序崩溃。


我的问题是,为什么不早点崩溃?它是如何工作的?


为什么我能设置rowptr [0] [5100]的值?


谢谢

#include< stdio.h>

#include< stdlib.h>


int main(void)

{

int nrows = 5,ncols = 10;

int row,i;

int ** rowptr;

rowptr = malloc(nrows * sizeof(int *));


for(row = 0; row< nrows; row ++)

rowptr [row] = malloc(ncols * sizeof(int));


rowptr [0] [5100] = 4;


for(i = 0; i< 30000; i ++){

printf("%d%d \ n",i,rowptr [0] [i]);

}

printf("%d \ n",rowptr [0] [5100]);

返回0;

}

Hello

This seems to work in my system, until variable ''i'' reaches 15985, then
the program crashes.

My question is, why won''t it crash sooner? How does it work?

And why am I able to set the value of rowptr[0][5100]?

Thanks
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int nrows = 5, ncols = 10;
int row, i;
int **rowptr;
rowptr = malloc(nrows * sizeof(int *));

for (row = 0; row < nrows; row++)
rowptr[row] = malloc(ncols * sizeof(int));

rowptr[0][5100] = 4;

for (i = 0; i < 30000; i++) {
printf("%d %d\n", i, rowptr[0][i]);
}
printf("%d\n", rowptr[0][5100]);
return 0;
}

推荐答案



jo ***** @ yahoo.com 写于04/07/06 11:02,:


jo*****@yahoo.com wrote On 04/07/06 11:02,:
你好

这似乎在我的系统中起作用,直到变化我达到15985,然后
程序崩溃。

我的问题是,为什么won''t它迟早崩溃?它是如何工作的?
Hello

This seems to work in my system, until variable ''i'' reaches 15985, then
the program crashes.

My question is, why won''t it crash sooner? How does it work?




爬上你的车,打开发动机,戴上眼罩,然后开始驾驶。你很可能会崩溃,但是你无法保证在离开车道的那一刻你会崩溃




-
Er*********@sun.com



Climb into your car, turn on the engine, put on
a blindfold, and start driving. You are quite likely
to crash, but there is no guarantee that you will crash
at the very instant you leave the driveway.

--
Er*********@sun.com


2006年4月7日08:02:07 -0700, jo*****@yahoo.com 写道:
On 7 Apr 2006 08:02:07 -0700, jo*****@yahoo.com wrote:
你好
这似乎在我的系统中工作,直到变量''我'达到15985,然后
我的问题是,为什么不早点崩溃?它是如何工作的?


您正在修改不属于属于的内存给你节目。

结果未定义。它可能会更快崩溃,它可能不会崩溃,

它可能会在不同的时间崩溃,这取决于系统中发生的其他事情,它可能会导致其他问题结束这个

程序后崩溃。

为什么我能设置rowptr [0] [5100]的值?
Hello
This seems to work in my system, until variable ''i'' reaches 15985, then
the program crashes.

My question is, why won''t it crash sooner? How does it work?
You are modifying memory that does not "belong" to you program. The
results are undefined. It may crash sooner, it may not crash at all,
it may crash at different times depending on what else is going on in
the system, it may cause something else to crash after you end this
program, and so on.
And why am I able to set the value of rowptr[0][5100]?




和以前一样,C不对数组/内存访问执行绑定检查。

编译器将生成代码来计算内存地址并尝试

写入它。在那个地址甚至可能没有任何真正的记忆。

某些操作系统/环境会检测到这一点并终止你的程序,而其他人不会。行为再次,未定义。


<访问未分配内存的代码已删除>



As before, C does not perform bound checks for array/memory access.
The compiler will produce code to compute a memory address and attempt
to write to it. There may even not be any real memory at that address.
Some operating systems / environments will detect this and terminate
your program, others will not. The behavior is again, undefined.

< code that access unallocated memory deleted >


< jo ***** @ yahoo.com> schrieb im Newsbeitrag

news:11 ********************** @ u72g2000cwu.googlegr oups.com ...
<jo*****@yahoo.com> schrieb im Newsbeitrag
news:11**********************@u72g2000cwu.googlegr oups.com...
你好

这似乎在我的系统中有效,直到变量''我'达到15985,然后
程序崩溃了。


这是预期的。

我的问题是,为什么它不会更快崩溃?它是如何工作的?


访问数组中每个单独对象的方式相同。

为什么我能设置rowptr [0] [5100]的值?


编译器不知道你分配了多少内存。

谢谢

#include< stdio.h>
#include< stdlib.h>

int main(void)
{n / n> = n,ncols = 10;
int row, i;
int ** rowptr;

rowptr = malloc(nrows * sizeof(int *));


您应该检查rowptr是否为NULL。如果malloc失败,它会让你(b = 0;行< nrows;行++)
rowptr [row] = malloc(ncols * sizeof(int));

rowptr [0] [5100] = 4;

for(i = 0; i< 30000; i ++){
printf("%d%d \\ \\ n,我,rowptr [0] [i]);
}
printf("%d \ n",rowptr [0] [5100]);
返回0 ;
}
Hello

This seems to work in my system, until variable ''i'' reaches 15985,
then
the program crashes.
That''s expected.
My question is, why won''t it crash sooner? How does it work?
The same way every individual object in an array is accessed.
And why am I able to set the value of rowptr[0][5100]?
The compiler doesn''t know how much memory you allocated.
Thanks
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int nrows = 5, ncols = 10;
int row, i;
int **rowptr;
rowptr = malloc(nrows * sizeof(int *));
You should check rowptr for NULL. If malloc fails it will make you
for (row = 0; row < nrows; row++)
rowptr[row] = malloc(ncols * sizeof(int));

rowptr[0][5100] = 4;

for (i = 0; i < 30000; i++) {
printf("%d %d\n", i, rowptr[0][i]);
}
printf("%d\n", rowptr[0][5100]);
return 0;
}



这篇关于访问其边界外的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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