为什么这个segc会出错?
我不明白为什么这个小小的C程序会出错:为什么这个segc会出错?,c,C,我不明白为什么这个小小的C程序会出错: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]){ int in = atoi(argv[1]); printf("Input %d\n",in); int *n = (int *)malloc(in); int j; for (j=0;j<in;j++)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
int in = atoi(argv[1]);
printf("Input %d\n",in);
int *n = (int *)malloc(in);
int j;
for (j=0;j<in;j++)
n[j] = j;
printf("Sanity check...\n");
char *c = (char *)malloc(1024*1024*20);
int i;
for (i=0; i<20*1024*1024;i++)
c[i] = i;
printf("No segfault. Yay!\n");
return 0;
}
#包括
#包括
int main(int argc,char*argv[]){
int in=atoi(argv[1]);
printf(“输入%d\n”,in);
int*n=(int*)malloc(in);
int j;
对于(j=0;j
您正在以
字节分配,而不是以
整数分配。请尝试:
malloc(sizeof(int) * in);
您的第二次分配有效,因为sizeof(char)
是1
此语句
int *n = (int *)malloc(in);
这是错误的
我想你是说
int *n = (int *)malloc(in * sizeof( int ) );
给定发布的代码,
让我们来分析一下它出现故障的原因。
我的评论中夹杂着代码
#包括
#包括
//未使用argc,因此编译器将发出警告
int main(int argc,char*argv[])
{
//未检查argc的值以确保参数存在
//因此,当不存在任何参数时,不会输出“用法”消息
//或者存在太多的参数
//注意:'in'是(IMO)局部变量的糟糕名称
int in=atoi(argv[1]);
//如果“in”大于0,则缺少确保值的检查
//当值也是时,处理错误时,不鼓励使用malloc
。@MichałSzydłowski为什么不鼓励使用malloc?如果没有有效的解释,你不能轻视有效的答案。我不轻视答案-这个答案纠正了错误,因此不再存在分段错误。它会响应关于这个问题。我的补充意见是,您编写的代码应该尽可能独立于类型-这意味着避免不必要的强制转换。在这里使用malloc
的强制转换无论如何都是隐式的,因此您可以让编译器毫无问题地执行它是的,谢谢你,我已经做了更改,它按预期工作。谢谢!我编写了这个小程序只是为了测试。它代表了我实际编写的流程,以便找出故障发生的位置和原因,而不必处理“生产”中的所有其他复杂性代码。您提到的格式化内容(例如检查argc值和处理错误)是在我正在编写的实际应用程序中处理的——我愚蠢地忽略了int数组的malloc,并从中了解到了不进行malloc类型转换。因此,感谢您的输入!
int *n = (int *)malloc(in);
int *n = (int *)malloc(in * sizeof( int ) );
given the posted code,
lets just run down the reasons it seg faults.
my comments are interspersed with the code
#include <stdio.h>
#include <stdlib.h>
// argc is not used, so the compiler will raise a warning
int main(int argc, char *argv[])
{
// the value of argc was not checked to assure a parameter is present
// so a 'usage' message was not output when no parameter is present
// or too many parameters are present
// note: 'in' is (IMO) a poor name for a local variable
int in = atoi(argv[1]);
// missing check to assure the value if 'in' is greater than 0
// and handling the error when the value is <= 0
printf("Input %d\n",in);
// in C, the returned value from calls to malloc(), and family,
// should not be cast
// the returned value is (per the definition of 'n')
// to be a pointer to an array of int's
// which should be written as:
// int *n = malloc( in * sizeof int );
// the returned value from malloc(), and family of functions,
// needs to always be checked (!= NULL)
// to assure the operation was successful
int *n = (int *)malloc(in);
int j;
// because the malloc call did not ask for a memory allocation
// of 'in' int's, so this code block corrupts the heap
// by writing past the end of the allocated memory
for (j=0;j<in;j++)
n[j] = j;
printf("Sanity check...\n");
// in C, the returned value from calls to malloc(), and family of functions,
// should not be cast
// the returned value needs to be checked (!= NULL) to assure the operation
// was successful
// the calculated value: 1024*1024*20 should be calculated by the
// compiler, not repeatedly at run time.
// so insert 'const int twentyMillion = 1024*1024*20;'
// at beginning of program
char *c = (char *)malloc(1024*1024*20);
int i;
// to avoid repeatedly calculating the 20million value, use a const (as above)
// so this line would be: for ( i=0; i<twentyMillion; i++ )
for (i=0; i<20*1024*1024;i++)
// the value of 'i' will massively exceed the value that can be kept in a char
// I.E. 255 so this loses data
// suggest replacing following line with: j = i%256; c[i] = j;
// to make it obvious what is going on
c[i] = i;
printf("No segfault. Yay!\n");
return 0;
} // end function: main