which function is better calloc or malloc ?

Jan 13, 2021 at 10:30am
It depends on what you want. calloc(...) additionally sets the memory to 0. See:

https://www.cplusplus.com/reference/cstdlib/calloc/
Jan 13, 2021 at 10:58am
Calloc also allows to specify the number of items and the size of each item. With malloc you have to explicitly do the calculation.

Is this for c or C++? In C++ don't use these. Use new/delete.
Jan 15, 2021 at 6:38am
For C++? Neither, you should be using new.

For C? Probably calloc since it initializes memory to 0 which is free or next to free on modern systems. This allows you to design data structures such that a zero-initialized struct is an invariant and "uninitialized" structs allocated on the heap are not possible. This is not necessary in C++ since we have the new operator and construtors, but that can be useful in C.
Topic archived. No new replies allowed.