cpointersmallocgoto

What is wrong in this function?


I guess there is problem with the relation of malloc and goto. Or, I guess there is some wastage of memory or corruption of memory happening out here. Hope, someone can point to me the exact error. When I compile its not giving me any error, but, my senior is insisting that I have a mistake.

#define FINISH() goto fini;

BOOL Do()
{

    BOOL stat;
    UINT32 ptr;
    int err;

    ptr = (UINT32)malloc(1000);


    free((void*)ptr);

fini:
    return stat;
}

Solution

  • Here are the problems I spotted in the code

    Here's the function with the edits I suggested

    BOOL Do()
    {
    
        BOOL stat = FALSE;
        size_t ptr = 0;
        int err;
    
        ptr = (UINT32)malloc(1000);
        err = Fun1();
    
        if (err != ERROR_SUCCESS || ptr == 0)
            FINISH();
        else
            stat = Fun2(ptr);
    
    fini:
        free((void*)ptr);
        return stat;
    }