code失败,分段故障 [英] Code fails with segmentation fault
问题描述
我在我的code得到分割故障失败。
我已经收窄code这个简化版本。因为在malloc的没有失败我已删除了明显的malloc检查。
当我尝试访问[0] do_something但是当我尝试访问相同的give_mem_and_do它不失败,我得到一个错误。
我不能对COM prehend的原因。
我传递一个已经在堆上分配一个位置的地址。
那么为什么它无法在访问这个位置。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT; ABC结构
{
INT *一个;
INT B:
}; typedef结构ABC的事情; INT do_something(事** XYZ,int类型的)
{
的printf(输入做点什么\\ n);
(* XYZ) - >一种[0] =一个;
返回0;
} INT give_mem_and_do(事** XYZ,为int *一)
{
INT RC;
的printf(\\ n进入功能give_mem_and_do \\ n);
如果(* XYZ == NULL)
{
* XYZ =(事*)malloc的(sizeof的(事));
(* XYZ) - >一种=为(int *)malloc的(的sizeof(int)的* 100);
}
的printf(调用do_something \\ n);
RC = do_something(XYZ,*一);
返回0;
} 诠释的main()
{
事* XYZ;
INT ABC = 1000; give_mem_and_do(安培; XYZ,&安培; ABC); #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT; ABC结构
{
INT *一个;
INT B:
}; typedef结构ABC的事情; INT do_something(事** XYZ,int类型的)
{
的printf(输入做点什么\\ n);
(* XYZ) - >一种[0] =一个;
返回0;
} INT give_mem_and_do(事** XYZ,为int *一)
{
INT RC;
的printf(\\ n进入功能give_mem_and_do \\ n);
如果(* XYZ == NULL)
{
* XYZ =(事*)malloc的(sizeof的(事));
(* XYZ) - >一种=为(int *)malloc的(的sizeof(int)的* 100);
}
的printf(调用do_something \\ n);
RC = do_something(XYZ,*一);
返回0;
} 诠释的main()
{
事* XYZ;
INT ABC = 1000; give_mem_and_do(安培; XYZ,&安培; ABC); 返回0;
}
以下是本次code的输出
进入功能give_mem_and_do
调用do_something
进入做点什么
分段错误(核心转储)
初始化 XYZ
在主
到 NULL
,因为
INT的main()
{
事* XYZ = NULL;
...
}
另外, * XYZ
不能为null和 give_mem_and_do
所需的指针就不会分配内存。
I am getting a segmentation fault failure in my code. I have narrowed down the code to this simplified version. I have removed the obvious malloc checks as there was no failure in malloc . I am getting an error when I try to access a[0] in do_something but when I try to access the same in the give_mem_and_do it doesnt fail. I am not able to comprehend the reason . I am passing the address of a location that is already allocated on the heap. So why should it in fail in accessing this location.
#include <stdio.h>
#include <stdlib.h>
struct abc
{
int *a;
int b;
};
typedef struct abc thing;
int do_something( thing ** xyz, int a)
{
printf ("Entering do something \n");
(*xyz)->a[0] = a;
return 0;
}
int give_mem_and_do (thing ** xyz, int *a)
{
int rc;
printf ("\n Entered function give_mem_and_do \n");
if (*xyz == NULL)
{
*xyz = (thing *)malloc ( sizeof (thing) );
(*xyz)->a = (int *) malloc (sizeof (int)*100);
}
printf (" Calling do_something \n");
rc = do_something (xyz, *a);
return 0;
}
int main ()
{
thing * xyz;
int abc = 1000;
give_mem_and_do (&xyz,&abc);
#include <stdio.h>
#include <stdlib.h>
struct abc
{
int *a;
int b;
};
typedef struct abc thing;
int do_something( thing ** xyz, int a)
{
printf ("Entering do something \n");
(*xyz)->a[0] = a;
return 0;
}
int give_mem_and_do (thing ** xyz, int *a)
{
int rc;
printf ("\n Entered function give_mem_and_do \n");
if (*xyz == NULL)
{
*xyz = (thing *)malloc ( sizeof (thing) );
(*xyz)->a = (int *) malloc (sizeof (int)*100);
}
printf (" Calling do_something \n");
rc = do_something (xyz, *a);
return 0;
}
int main ()
{
thing * xyz;
int abc = 1000;
give_mem_and_do (&xyz,&abc);
return 0;
}
Following is the output of this code
Entered function give_mem_and_do
Calling do_something
Entering do something
Segmentation fault (core dumped)
Initialize xyz
in main
to NULL
, as
int main ()
{
thing * xyz = NULL;
...
}
Otherwise, *xyz
may not be NULL andgive_mem_and_do
will not allocate memory for required pointers.
这篇关于code失败,分段故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!