简体   繁体   中英

pointer/struct free() - Heap Corruption

I have a small, but annoying, problem here.

So here are my structures.

typedef struct tElem {
    struct tElem *ptr;
    int data;
} *tElemPtr;


typedef struct {
    tElemPtr Act;
    tElemPtr First;
} tList;

And here is the allocation. This is where the heap corruption occurs.

tElemPtr newElemPtr = (tElemPtr)malloc(sizeof(tElemPtr));
.
.
.
free(newElemPtr);

But it doesn't occur, when I use size of the actual structure.

tElemPtr newElemPtr = (tElemPtr)malloc(sizeof(struct tElem));
.
.
.
free(newElemPtr);

Could somebody please explain to me, what am I missing here?

Thank you.

It's because you are mallocing a pointer not a new struct

sizeof(tElemPtr) is going to return the size of a pointer not the size of the struct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM