It seems as a good programing practice to check each time after using malloc/calloc if an address was asagin.
Is there an option to create a function to check if the allocation succeed? as we cast we cast the type of the point each time, so the function will not know which pointer type is it.
For example:
newUser -> name = (char*)malloc(NAME_LENGTH*sizeof(char));
if (newUser -> name == NULL){
printf("Allocation of newUser failed\n");
exit(1);
}
User *newUser = (User*)malloc(sizeof(User));
if(newUser == NULL){
printf("Allocation of newUser failed\n");
exit(1);
}
Can a function be created that gets newUser and newUser -> name and will exit if allocation failed?