3 ms·
It's handy in C for guarding parts of the language, as you often have to check return values anyway. if(malloc(10000 * sizeof foo) == NULL) { // Erro
by shakna 8y ago
It's handy in C for guarding parts of the language, as you often have to check return values anyway.
if(malloc(10000 * sizeof foo) == NULL) {
// Error processing
} // No need for an else branch here.
- the-dude 8y agoMissing assingment.
- shakna 8y agoint success = NULL; if(success = malloc(...)) { } if(success = malloc(...)) { } if(success = malloc(...)) { }
- slavik81 8y agoThat will result in undefined behaviour if the value returned by malloc is greater than INT_MAX. If you really need an integer, use intptr_t. But, generally it makes more sense to just use a pointer. char* p = NULL; if (p = malloc(...)) { ... }