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;
}
Here are the problems I spotted in the code
err != ERROR_SUCCESS
this function will leak memory. It will jump over the free
call. malloc
into a 32 bit location. This is not a portable solution. On 64 bit platforms this will wreak havoc on your program as you'd be truncating the address. If you must use a non-pointer type here use size_t
instead (although I would reccomend a pointer over an integral type) stat
is not definitively assigned here. You are returning garbage if err != ERROR_SUCCESS
. It needs to always be assigned a value. Easiest way is provide a default.malloc
and potentially pass a hidden NULL
pointer into Fun2
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;
}