gpt4 book ai didi

c - 运行时错误(SIGSEGV)

转载 作者:行者123 更新时间:2023-11-30 21:37:22 25 4
gpt4 key购买 nike

我是编码新手。当我在 codecheff 中提交代码时,它给出“运行时错误(SIGSEGV)”。我不知道有什么问题请帮忙。提前致谢。

int call(int *x, int m)
{
int b[10], y, z;
for(y = 0 ; y<m ; y++)
{
int sum = 0;
z = *x;
while(z>0) {
sum = sum + z/5;
z=z/5;
}
b[y] = sum;
x++;
}

for(y = 0 ; y<m ; y++)
printf("\n%d", b[y]);
}

int main()
{
int n=0, i=0, a[10];
scanf("%d", &n);
for(i=0;i<n;i++){
scanf("%d", &a[i]);
}
call(a, n);
return 0;
}

最佳答案

如果您收到 SIGSEGV,则意味着您正在尝试访问程序无权访问的段中的内存,或者您正在尝试以无效方式访问程序有权访问的内存。

当您遇到这样的内存段错误时,您的第一个行动应该是使用 Valgrind 或 Dr. Memory。

根据您的代码,我假设当 n > 10 时您可能会遇到问题,导致 b 和 x 中的缓冲区溢出。如果您将参数命名为有意义的名称,我们会更容易解决。

关于c - 运行时错误(SIGSEGV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33616220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com