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
Aucun commentaire:
Enregistrer un commentaire