[Olsr-dev] Good use patterns of "goto" - or not?

Bernd Petrovitsch (spam-protected)
Tue Nov 18 17:58:36 CET 2008


On Tue, 2008-11-18 at 16:54 +0100, Roar Bjørgum Rotvik wrote:
> Alina Friedrichsen wrote:
> > Hi Bernd!
> >> [...]
> >> freexyz_and_out:
> >> 	free(z);
> >> freexy_and_out:
> >> 	free(y);
> >> freex_and_out:
> >> 	free(x);
> >> 	return rv;
> >> }
> 
> > I write often the following:
> > [...]
> > failure:
> > 	if(x != NULL) free(x);
> > 	if(y != NULL) free(y);
> > 	if(z != NULL) free(z);
> > 	return false;
> > }
> 
> I also likes this approach a lot better than copying the error handing in every code 
> branch. The goto call in it self is not evil or dangerous, it all depend on how it is used.

ACK - as with every tool.....
Alas, there are tools which make it way too easy to shot yourself in the
foot (or knee).

> But I don't think a check for NULL before calling free() is needed, most operating system 

"free(NULL)" is a no-op as far as the C standard is concerned.

> as I know of handles a NULL pointer just fine (please correct me if you know of any system 
> that does not).

These should fix their C-lib implementation.

>  From Linux "man 3 free":
> "If ptr is NULL, no operation is performed."
> 
>  From MS MSDN (windows):
> "If memblock is NULL, the pointer is ignored and free immediately returns."

	Bernd
-- 
Firmix Software GmbH                   http://www.firmix.at/
mobil: +43 664 4416156                 fax: +43 1 7890849-55
          Embedded Linux Development and Services






More information about the Olsr-dev mailing list