Ad

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

Ad

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:

  1. In C, you should not cast the return value from malloc. See this question for more information.
  2. 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.
  3. After attempting to allocate all of the memory, you can use a single if to check whether all of the allocations succeeded.
  4. Passing a NULL pointer to free 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 to free (without bothering to check if it's NULL).
  5. exit(0) is used to inform the shell that the program succeeded. Calling exit with a non-zero value (typically 1) indicates failure. The macro EXIT_FAILURE from <stdlib.h> is the official non-zero value that should be passed to exit to indicate failure.
Ad
source: stackoverflow.com
Ad