I have a question about an example treated in this site (define a function returning struct pointer) :
struct Person {
char *name;
int age;
int height;
int weight;
};
struct Person *Person_create(char *name, int age, int height, int weight)
{
struct Person *who = malloc(sizeof(struct Person));
assert(who != NULL);
who->name = strdup(name);
who->age = age;
who->height = height;
who->weight = weight;
return who;
}
The size performed by "sizeof(struct Person)" is 16 bytes for a 32 bits MCU (4 bytes for the pointer, and 3 Integers of 4 bytes). So the "malloc" function allocates 16 bytes in RAM for a new instance of the structure "Person". But in fact, when we make an affectation of "Person", the space really used for an instance of "Person" is different and can be more important, this depend of length of the "name" parameter.
Question: the divergence between the size allocated by the "malloc" function and the size really taken for an instance of "Person" could it be a problem ?
Best regards.