c - Do I cast the result of malloc

Mots clés : cmalloccastingc

meilleur 3 Réponses c - Do I cast the result of malloc

vote vote

92

int *sieve = (int *) malloc(sizeof(int) * length); 
int *sieve = malloc(sizeof *sieve * length); 
int *sieve = malloc(length * sizeof *sieve); 
int *sieve = malloc(sizeof *sieve * length); 
vote vote

81

int *sieve = malloc(sizeof *sieve * length); 
vote vote

71

#ifdef __cplusplus # define NEW(type, count) ((type *)calloc(count, sizeof(type))) #else # define NEW(type, count) (calloc(count, sizeof(type))) #endif 
int *sieve = NEW(int, 1); 

Questions similaires