Re: Memory leak when internal pointer passed out as parameter



Mike wrote:

I have following existing code. And there is memory leak. Anyone
know how to get ride of it? function foo has been used in thousands
places, the signature is not allowed to change.

my_struc * foo1( )
{
my_struc * tmp;

tmp = (my_struc *)calloc(1, sizeof(my_struc));
return tmp;
}

void main()
{
my_struc *mainPtr;

mainPtr = foo1();
free(mainPtr);
}

No leak exists. However that will not compile. There is no
declaration for my_struc, there is no #include <stdlib.h>, the
declaration of main is invalid. In addition the cast in the call
to calloc is unnecessary and foolish, and hides other errors.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

.