[Discuss] Casting the return value of malloc bad?

Daniel M. German dmgerman at uvic.ca
Sun Jul 16 14:40:38 PDT 2006


I partially endorse Paul's method. I have been doing a lot of code 
refactoring of a C system and I find that this is the cleaner way to
avoid errors because all the information is there. If you use:

foo = malloc(n * sizeof(type_of_foo));

the problem is that you need to go and check that foo is declared as:

type_of_foo *foo;


 Paul> Do it this way:
 Paul> foo = malloc(n * sizeof(*foo));

But I still prefer another way:

foo = calloc(n, sizeof(*foo));

The speed penalty is negligible.



--
Daniel M. German
http://turingmachine.org/
http://silvernegative.com/
dmg (at) uvic (dot) ca
replace (at) with @ and (dot) with .

 


More information about the Discuss mailing list