What’s better/more efficient – calloc or malloc?
I want to initialise structs, that refer to other instances of the same struct also
VARIANT 1
person *new_person() {
struct _person *person = calloc(1, sizeof(person));
person->name = NULL;
person->child = NULL;
return person;
}
VARIANT 2
person *new_person() {
struct _person *person = malloc(sizeof(person));
person->name = NULL;
person->child = NULL;
return person;
}
The struct
typedef struct _person {
*void name;
struct _person *child;
} person;
>Solution :
There are obvious and more subtile problems in your examples:
-
*void name;is a syntax error. -
Neither
struct _person* person = calloc(1, sizeof(person));norstruct _person* person = malloc(sizeof(person));will allocate the correct amount of memory becausesizeof(person)will evaluate to the size of the pointerpersonin the definition, not the typepersondefined as a typedef forstruct _person.This is a pathological example of name shadowing, whereby a local definition hides another definition for the same identifier in an outer scope. Use
-Wshadowto let the compiler detect and report this kind of problem.
In both examples, you should the size of the data pointed to by the pointer:
struct _person *person = calloc(1, sizeof(*person));
Regarding whether to use calloc() or malloc(), it is much safer to always use calloc() for these reasons:
- the size of the element and the number of elements are separate, thus avoiding silly mistakes in the overall size computation.
- the memory is initialized to all bits zero, which is the zero value of all scalar types on modern systems. This may allow you to simplify the code by omitting explicit initialization of integer members and will force initialization to
0of all extra members later added to the structure definition for which the allocation functions might be missing initializing statements. - for large arrays,
calloc()is actually faster thanmalloc(size)+memset(s, 0, size), as is documented this answer: https://stackoverflow.com/a/18251590/4593267
A more general discussion of this topic is this other question:
Difference between malloc and calloc?