无法释放结构的数据

问题描述 投票:-1回答:1

我将此结构作为程序的一部分,并带有它的创建和销毁功能,但是,当我尝试释放结构实例时,程序由于与释放相关的错误而崩溃,例如代码中的main会因“ munmap_chunk():无效的指针”而崩溃”

I don't understand why the pointers are invalid,
any help please?

p.s. what would be the best way to initialize new in euCreate()? Everything I try either causes a memory leak or dosen't work 

        Eu euCreate()
{
    Eu new = malloc(sizeof(new));
    (*new).Judges = malloc(sizeof(Judge) * 2);
    (*new).Jlen = 2;
    (*new).Jidx = 0;
    (*new).States = malloc(sizeof(State) * 2);
    (*new).Slen = 2;
    (*new).Sidx = 0;
    return new;
}

void euDestroy(Eu eu)
{
    free(eu->Judges);
    free(eu->States);
}

int main()
{
    Eu eu = euCreate();
    euDestroy(eu);
}
typedef struct eu{
struct judge* Judges;
int Jlen;
int Jidx;
struct state* States;
int Slen;
int Sidx;

} * Eu;

c memory malloc free
1个回答
0
投票
Eu new = malloc(sizeof(new));
© www.soinside.com 2019 - 2024. All rights reserved.