Should I Use Both If And Else Conditions When Dynamically Allocating Memory In C?
int main(){
int* index = (int*)malloc(n*sizeof(int));
if(index == NULL){
printf("Memory Not Allocated.\n");
exit(0);
}
int* arr = (int*)malloc(p*sizeof(int));
if(arr == NULL){
printf("Memory Not Allocated.\n");
exit(0);
}
int* queue = (int*)malloc(frames*sizeof(int));
if(queue == NULL){
printf("Memory Not Allocated.\n");
exit(0);
}
more codes...
---------------
---------------
more codes....
}
This is a part of my program. In this code I have dynamically allocated memory to index, arr and queue. Also I have used if condition for check weather the memory is allocated or not. I only used if part. I didn't used else part because I have used exit(0) inside of if condition. Is this way correct? or should I also use else part like below?
int main(){
int* index = (int*)malloc(n*sizeof(int));
if(index == NULL){
printf("Memory Not Allocated.\n");
exit(0);
}else{
int* arr = (int*)malloc(p*sizeof(int));
if(arr == NULL){
printf("Memory Not Allocated.\n");
exit(0);
}else{
int* queue = (int*)malloc(frames*sizeof(int));
if(queue == NULL){
printf("Memory Not Allocated.\n");
exit(0);
}else{
more codes...
---------------
---------------
more codes....
}
}
}
As I suppose program will exit if the memory is not allocated because of exit(0). Therefore, it's not necessary to use else part. That's my opinion.
What is the correct and better method? Are these both ways are okay to use?
Thank you so much for your help
Answer
As I suppose program will exit if the memory is not allocated because of exit(0). Therefore, it's not necessary to use else part.
Yes, that is correct. The else
is not necessary, and using else
makes the code harder to read. So it should not be used.
The method I would use is shown below:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *index = malloc(n*sizeof(int));
int *arr = malloc(p*sizeof(int));
int *queue = malloc(frames*sizeof(int));
if (index == NULL || arr == NULL || queue == NULL) {
printf("Memory Not Allocated.\n");
free(index);
free(arr);
free(queue);
exit(EXIT_FAILURE);
}
more codes...
---------------
---------------
more codes....
}
Things to note:
- In C, you should not cast the return value from
malloc
. See this question for more information. - You can defer the error checking until all of the memory is allocated. The expectation is that all of the allocations will succeed. In the unlikely event that one or more allocations fail, the code will waste a little time before giving the user the bad news. But otherwise there's no harm in trying all of the allocations. The user is unlikely to notice the extra time, and will be much more concerned about the fact that the program didn't run.
- After attempting to allocate all of the memory, you can use a single
if
to check whether all of the allocations succeeded. - Passing a
NULL
pointer tofree
is specifically allowed by the C standard, so as long as you've initialized each pointer (either with a valid address or NULL), you can pass each pointer tofree
(without bothering to check if it's NULL). exit(0)
is used to inform the shell that the program succeeded. Callingexit
with a non-zero value (typically 1) indicates failure. The macroEXIT_FAILURE
from<stdlib.h>
is the official non-zero value that should be passed toexit
to indicate failure.
Related Questions
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page