Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When to use calloc or malloc in C

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)); nor struct _person* person = malloc(sizeof(person)); will allocate the correct amount of memory because sizeof(person) will evaluate to the size of the pointer person in the definition, not the type person defined as a typedef for struct _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 -Wshadow to 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 0 of 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 than malloc(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?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading