Reputation: 13
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.
Upvotes: 0
Views: 79
Reputation: 6050
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.
Upvotes: 1