|
发表于 2003-4-22 02:20:09
|
显示全部楼层
Re: 奇怪的segmentation fault !gcc
[quote:694f95034c="genius_DWH"]这是我在turboc下面调试运行通过的一个程序,在gcc下就说是segmentation fault ,我不知这句话什么意思?还有这个程序为什么不能通过?请那位高手帮忙看看?
[code:1]
#include <stdio.h>
#include <stdlib.h>
main()
{
static int hist[13]={0,0,0,0,0,0,0,0,0,0,0,0,0};
int x,y,k,j;
for (j=1;j<=5000;j++)
{
[b]x=(int)(6*(rand()/32767.1)+1);[/b]
[b]y=(int)(6*(rand()/32767.1)+1);[/b]
[b]hist[x+y]++;[/b]
}
for (j=2;j<=12;j++) {
printf("%2d :",j);
for (k=1;k<=hist[j]/20;k++)
{
printf("*");
}
printf("\n");
}
}[/code:1][/quote]
[code:1]
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int
main (void)
{
static int list[13]={
0,0,0,0,0,0,0,0,0,0,0,0,0};
int x,y,k,j;
srand ((unsigned )time(NULL));
for (j=1; j <=5000; j ++){
x = (int)(6 * (rand()/(double)(RAND_MAX)+1.0));
y = (int)(6 * (rand()/(double)(RAND_MAX)+1.0));
printf ("%d %d \n", x, y);
list[x+y]++;
}
printf ("%d \n", RAND_MAX);
for (j=2; j <= 12; j++){
printf ("%2d:", j);
for (k=1; k <= list[j]/20; k ++)
printf ("*");
printf ("\n");
}
return 0;
}
[/code:1]
//
很显然是随机数字生成错误,造成了对内存的错误访问!
注释掉那个hist[x+y]++, 用printf 输出x, y值看看就知道了 !!
//
不 知道你的 那个32767.1是什么意思,
我用了 RAND_MAX 宏,
这样生成的随机数字就对了!! |
|