Using valgrind to read this I get: Invalid write/read of size 4
struct Person{
char* name;
int age;
};
struct Person* create_person(char *name, int age)
{
struct Person* me = (struct Person*)malloc(sizeof(struct Person*));
assert(me!= NULL); //make sure that the statement is not null
me->name = name;
me->age = age;
return me;
}
Using this got clean log with valgrind
struct Person{
char* name;
int age;
};
struct Person* create_person(char *name, int age)
{
struct Person* me = (struct Person*)malloc(sizeof(struct Person*)+4);
assert(me!= NULL); //make sure that the statement is not null
me->name = name;
me->age = age;
return me;
}
Why should I explicitly put sizeof(struct+intSize)
to avoid this error? sizeof
don't get the whole size of a struct?