分段错误 - 数组的有趣问题 [英] Segmentation fault - interesting problem with array

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

问题描述

嗨朋友们,

我使用的是Mandriva Linux 9.2和gcc。


我的源代码是,


int chunkin [7225] [9]; //没有错误

int i;

for(i = 0; i< 7225; i ++)

{

chunkin [i] [0] = somedata;

- -

- -

chunkin [i] [8] = somedata;

}


这会产生分段错误。高达i = 5439(5440值),它是
正常工作。在此之后,它给出了分段错误。这是

阵列可以容纳的最大数据量吗?如果是这样,我应该怎么做

我的数组保存7225 * 9数据([7225] [9])

Hi friends,
I am using Mandriva Linux 9.2 and gcc.

My source code is,

int chunkin[7225][9] ; //no error
int i ;
for (i=0;i<7225;i++)
{
chunkin[i][0] = somedata ;
- -
- -
chunkin[i][8] = somedata ;
}

This gives Segmentation fault. Upto i = 5439 (5440 values ) it is
working fine. After this it is giving Segmentation fault. Is this the
maximum number of data an array can hold ? If so, What should I do for
my array to hold 7225 * 9 data ([7225][9])

推荐答案

Sameer写道:
嗨朋友们,
我使用的是Mandriva Linux 9.2和gcc。


不应该相关 - 如果是,你可能是OT。

我的源代码是,

int chunkin [7225] [9]; //没有错误
int i;
for(i = 0; i< 7225; i ++)
{
chunkin [i] [0] = somedata;
- -
- -
chunkin [i] [8] = somedata;
}
这会产生分段错误。高达i = 5439(5440值)它工作正常。在此之后,它给出了分段错误。这是数组可容纳的最大数据量吗?如果是这样,我该怎么做才能使我的阵列保存7225 * 9数据([7225] [9])
Hi friends,
I am using Mandriva Linux 9.2 and gcc.
Should not be relevant -- and if is, you''re probably OT.

My source code is,

int chunkin[7225][9] ; //no error
int i ;
for (i=0;i<7225;i++)
{
chunkin[i][0] = somedata ;
- -
- -
chunkin[i][8] = somedata ;
}

This gives Segmentation fault. Upto i = 5439 (5440 values ) it is
working fine. After this it is giving Segmentation fault. Is this the
maximum number of data an array can hold ? If so, What should I do for
my array to hold 7225 * 9 data ([7225][9])



看起来很可疑。


你能否提供一个可编辑的,可运行的代码片段,表现出这种行为?
HTH,

- g


-

Artie Gold - 德克萨斯州奥斯汀
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays

如果你没有什么可隐瞒的,你就不会尝试!


This looks fishy.

Could you provide a compilable, runnable, snippet of code that exhibits
this behavior?

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"


你知道如何查看正确的头文件以获得最大的数组大小吗?

就可移植性而言,这是你应该考虑的事情。

我同意Artie Gold;为什么你不展示一个完整的,完整的程序。

Do you know how to view the right header file for maximum array sizes?
And in terms of portability, it is something you should consider.
I agree with Artie Gold; why don''t you show a whole, complete program.


Sameer写道:

int chunkin [ 7225] [9]; //没有错误
int i;
for(i = 0; i< 7225; i ++)
{
chunkin [i] [0] = somedata;
- -
- -
chunkin [i] [8] = somedata;
}
这会产生分段错误。 upto i = 5439(5440值)它
工作正常。在此之后,它给出了分段错误。这是数组可容纳的最大数据量吗?如果是这样,我应该为我的阵列做什么来保存7225 * 9数据([7225] [9])

int chunkin[7225][9] ; //no error
int i ;
for (i=0;i<7225;i++)
{
chunkin[i][0] = somedata ;
- -
- -
chunkin[i][8] = somedata ;
}

This gives Segmentation fault. Upto i = 5439 (5440 values ) it
is working fine. After this it is giving Segmentation fault. Is
this the maximum number of data an array can hold ? If so, What
should I do for my array to hold 7225 * 9 data ([7225][9])



我的猜测是改变int chunkin。 " tostatic int

chunkin ......会治愈它。无论如何,您定义的对象比B标准要求系统提供的更大。
。我认为所需的大小是65535字节。


大多数系统比这更好,但通常限制在

他们可以提供的自动存储量。当您将

项目设为静态时,系统有更好的机会安排

存储空间。


打印超出sizeof chunkin的价值(a size_t,不是整数)

来了解你的要求。


-

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

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

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

回复在文章标题的底部。 - Keith Thompson

详情请见:< http://cfaj.freeshell.org/google/>


My guess is that changing "int chunkin.." to "static int
chunkin..." will cure it. In any case you are defining an object
larger that what the C standard requires you system to provide. I
think the required size is 65535 bytes.

Most systems do better than this, but are usually limited in the
amount of automatic storage they can provide. When you make the
item static the system has a better chance of arranging for the
storage to be present.

Print out the value of "sizeof chunkin" (a size_t, not an integer)
to get an idea of what you are asking for.

--
"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." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>


这篇关于分段错误 - 数组的有趣问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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