关于OJ中的Segmentation Fault

yiloving 2015-06-15 08:26:14
这段代码用VS2013编译运行都是正确的,提交出现Segmentation Fault 错误,

Segmentation Fault : The possible cases of your encountering this error are:

•1.buffer overflow --- usually caused by a pointer reference out of range.

•2.stack overflow --- please keep in mind that the default stack size is 8192K.

到底是哪个地方出错了?求教了
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

int
main()
{
int n, m, count[10], *buf;
int i, k;
scanf("%d", &n);
memset(count, 0, n*sizeof(int));
for (i = 0; i < n; i++)
{
scanf("%d", &m);
buf = (int *)malloc(m*sizeof(int));
if (buf == NULL)
{
printf("space error\n");
return 0;
}
int j = 0;
while ( m != 0)
{
scanf("%d", &buf[j]);
if (buf[j] < 0 || buf[j] > 9999)
{
printf("error");
return 0;
}
if (buf[j] > 6000 && buf[j] <= 9999)
count[i]++;
j++;
m--;
}
}
for (k = 0; k < n; k++)
printf("%d\n", count[k]);
return 0;
}
...全文
418 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly_dragon_fly 2015-06-16
  • 打赏
  • 举报
回复
int count[1000000];这个作全局试试
mxway 2015-06-15
  • 打赏
  • 举报
回复
不知道你n的取值范围是多少

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧