[英]Segmentation fault in C, malloc array
I have problem with my school project, the server which is testing this build returns this error: 我的学校项目有问题,正在测试此版本的服务器返回以下错误:
./__runscript: line 2: 489 Segmentation fault ./a.out < __input
Compile is OK, but this error is displayed just when it starts running. 编译正常,但是仅在开始运行时显示此错误。 But if I run it on Windows 7 with visual studio 10, everything is fine.
但是,如果我在带有Visual Studio 10的Windows 7上运行它,一切都很好。 I think the mistake might be in the insert() function.
我认为错误可能出在insert()函数中。
POSTUP *insert(POSTUP *first)
{
POSTUP* current,*pom;
current=(POSTUP*)malloc(sizeof(POSTUP));
pom=(POSTUP*)malloc(sizeof(POSTUP));
int i,count_of_elements;
scanf("%d", &count_of_elements);
if(first==NULL)
{
first=current;
first->array= (int*)malloc(count_of_elements*sizeof(int));
for(i=0; i<count_of_elements; i++)
{
scanf("%d",&first->array[i]);
}
first->elements=count_of_elements;
first->next=NULL;
return first;
}
else
{
pom->array= (int*)malloc(count_of_elements*sizeof(int));
for(i=0; i<count_of_elements; i++)
{
scanf("%d",&pom->array[i]);
}
pom->next=NULL;
pom->elements=count_of_elements;
current=first;
while(1)
{
if(current->next==NULL)
{
current->next=pom;
return first;
}
}
}
return 0;
}
int main(void)
{
int count, i;
POSTUP* first,*current;
first= (POSTUP*)malloc(sizeof(POSTUP));
first=NULL;
scanf("%d",&count);
for(i=0; i<count; i++)
{
first=insert(first);
}
}
This code in main()
: 这段代码在
main()
:
POSTUP *first, *current;
first = (POSTUP*)malloc(sizeof(POSTUP));
first = NULL;
scanf("%d", &count);
for (i = 0; i < count; i++)
{
first = insert(first);
}
is clearly problematic. 显然是有问题的。 You allocate space and store the only pointer to it in
first
; 您分配空间和存储的唯一指针,它
first
; you then overwrite the pointer, leaking memory. 然后,您将覆盖指针,从而泄漏内存。 You should officially check that the allocation was not null.
您应该正式检查分配是否为空。 But your
insert()
code is equipped to handle a null pointer as input, so the memory allocation in main is redundant. 但是您的
insert()
代码可以处理空指针作为输入,因此main中的内存分配是多余的。
In insert()
, you allocate memory without checking for success; 在
insert()
,您分配内存而不检查是否成功; this is a recipe for trouble, though more usually later than sooner. 这是一个麻烦的秘诀,尽管通常比迟早发生。
Your code is never checking the result of scanf()
; 您的代码永远不会检查
scanf()
的结果; that means you could be getting rubbish into your system if there's a format error in the data. 这意味着,如果数据中存在格式错误,则可能会使您的系统陷入混乱。
Likely cause of crash: In the 'if (first == NULL) block in
insert()`, you have: 可能的崩溃原因:在insert()中的'if(first == NULL)
block in
,您具有:
for(i=0; i<count_of_elements; i++)
{
scanf("%d", &prvy->array[i]);
}
There's no declaration of prvy
in sight in the code you show; 您显示的代码
prvy
不到prvy
的声明; it should almost certainly be referring to &first->array[i]
anyway. 无论如何,它几乎应该肯定是指
&first->array[i]
。 This could easily be good for a crash. 这很容易崩溃。 Otherwise, you've carefully stashed the data in a different set of memory than you thought, so the array for
first
is uninitialized gibberish. 否则,您已将数据小心地保存在与您想象的不同的内存集中,因此
first
数组是未初始化的乱码。
The same code does not use pom
, so you've leaked memory in this part of the code. 相同的代码不使用
pom
,因此您在这部分代码中泄漏了内存。
In the main else
clause in insert()
, you overwrite current
(which contained a memory allocation) with first
, thus leaking memory there too. 在
insert()
的main else
子句中,您用first
覆盖了current
(包含内存分配),从而导致那里的内存泄漏。
Don't allocate memory until you're about to use it. 在要使用它之前不要分配内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.