Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 无结构元素函数_C_Segmentation Fault_Structure_Free - Fatal编程技术网

C 无结构元素函数

C 无结构元素函数,c,segmentation-fault,structure,free,C,Segmentation Fault,Structure,Free,我不明白为什么这是seg的错。这是我进入建筑的方式吗? 在freelem函数中,您对不是通过malloc-系列函数获得的指针调用free。这会导致未定义的行为 要解决此问题,请删除那些免费的调用;或者在main中分配给a和b时,使用malloc-系列功能 另外,malloc(sizeof(Date))应该是malloc(sizeof(name)),而yoy应该free(x)在main的末尾(或者freelem的末尾)。如果我不能使用malloc,如果它们已经分配到其他地方,我想释放它们怎么办?我

我不明白为什么这是seg的错。这是我进入建筑的方式吗?
freelem
函数中,您对不是通过
malloc
-系列函数获得的指针调用
free
。这会导致未定义的行为

要解决此问题,请删除那些
免费的
调用;或者在
main
中分配给
a
b
时,使用
malloc
-系列功能


另外,
malloc(sizeof(Date))
应该是
malloc(sizeof(name))
,而yoy应该
free(x)
main
的末尾(或者
freelem
的末尾)。

如果我不能使用malloc,如果它们已经分配到其他地方,我想释放它们怎么办?我知道类型和结构。@ConstantinMariusManuel
free
的工作方式是你只能释放你想要的东西。别无选择
typedef struct //this is a some structure
{
    char *a,*b;
    float x;
}name;
void freeelem(void *x) //the function for element mem free
{
    free(((name*)x)->a);
    free(((name*)x)->b);
}
void* allocelem() // 
{
    void *aux;
    aux=malloc(sizeof(name));
    return aux;
}
int main()
{
    void *x=allocelem();
    ((name*)x)->a="fdasf";
    ((name*)x)->b="fafas";
    freeelem(x);
    return 0;
}