I have seen a few different ways of doing malloc error checking? Is one way better than the other? Are some exit codes better than others? Is using fprintf with stderr better than using a printf statement? Is using a return instead of an exit better?

    ptr=(int*)malloc(n*sizeof(int));  //memory allocated using malloc
    if(ptr==NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(0);
    }

    ptr=(int*)malloc(n*sizeof(int));  //memory allocated using malloc
    if(ptr==NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(1);
    }

    res = malloc(strlen(str1) + strlen(str2) + 1);
    if (!res) {
        fprintf(stderr, "malloc() failed: insufficient memory!\n");
        return EXIT_FAILURE;
    }

    ptr=(int*)malloc(n*sizeof(int));  //memory allocated using malloc
    if(ptr==NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(-1);
    }

   ptr=(int*)malloc(n*sizeof(int));  //memory allocated using malloc
    if(ptr==NULL)                     
    {
        printf("Error! memory not allocated.");
        exit(EXIT_FAILURE);
    }

char *ptr = (char *)malloc(sizeof(char) * some_int);
if (ptr == NULL) {
    fprintf(stderr, "failed to allocate memory.\n");
    return -1;
}

char* allocCharBuffer(size_t numberOfChars) 
{
    char *ptr = (char *)malloc(sizeof(char) * numberOfChars);
    if (ptr == NULL) {
        fprintf(stderr, "failed to allocate memory.\n");
        exit(-1);
    }
    return ptr;
}

First and foremost, you don't need to cast the return value of malloc. In fact, doing so can hide legitimate errors such as failing to include <stdlib.h>. Second, sizeof(char) is guaranteed to be 1 in all cases, so things like sizeof(char) * some_int are unnecessarily complex.

I have seen a few different ways of doing malloc error checking? Is one way better than the other?

As for checking the result, both x == NULL and !x will work; it's really a stylistic matter of which one you find more informative.

Are some exit codes better than others?

The only codes guaranteed to be portable are 0, EXIT_SUCCESS, and EXIT_FAILURE. But before you use 0 or EXIT_SUCCESS, keep in mind that both are treated as success codes. Why exit with 0, for example, if the program terminated unsuccessfully?

Is using fprintf with stderr better than using a printf statement?

Yes. printf is forced to stdout, but what if you want your errors to go to a log file rather than standard output along with non-error output? fprintf and similar give you more flexibility in error reporting.

Is using a return instead of an exit better?

Both are pretty awful in general, and it's because terminating on an allocation failure instead of trying to recover is usually not the most robust approach.

When you identify a blunder with malloc(), calloc() and realloc() (i.e they give back a NULL pointer), errno is set. You can then utilize the standard capacity perror() to print the blunder without the need to do your own organizing. Note that it will naturally print to stderr, no compelling reason to trouble with that.Further more, If your application considers the mistake as deadly, then the procedure must be finished. In the event that your code is situated in the fundamental() capacity, then utilizing return EXIT_FAILURE; is fine, and utilize exit(EXIT_FAILURE); if not. It is not prescribed to exit with your own arrival code all things considered. In the event that the blunder is not considered as lethal, then it's dependent upon you how to handle it.Please likewise take note of that, when realloc() comes up short and returns NULL, the old pointer is still legitimate and should subsequently be liberated before taking off.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.