[Olsr-dev] Good use patterns of "goto" - or not? (was Re: httpinfo IPv6 patch)

Alina Friedrichsen (spam-protected)
Tue Nov 18 16:17:08 CET 2008


Hi Bernd!

> int x(...)
> {
> 	int rv = -1;
> [...]
> 	x = malloc(a);
> [...]
> 	if(some_error) {
> 		goto freex_and_out;
> 	}
> [...]
> 	y = malloc(b);
> [...]
> 	if(some_error) {
> 		goto freexy_and_out;
> 	}
> [...]
> 	z = malloc(c);
> [...]
> 	if(some_error) {
> 		goto freexyz_and_out;
> 	}
> [...]
> 	rv = 0;
> freexyz_and_out:
> 	free(z);
> freexy_and_out:
> 	free(y);
> freex_and_out:
> 	free(x);
> 	return rv;
> }

I write often the following:

bool foobar(...)
{
	void *x = NULL;
	void *y = NULL;
	void *z = NULL;
[...]
	x = malloc(a);
[...]
	if(some_error) {
		goto failure;
	}
[...]
	y = malloc(b);
[...]
	if(some_error) {
		goto failure;
	}
[...]
	z = malloc(c);
[...]
	if(some_error) {
		goto failure;
	}
[...]
failure:
	if(x != NULL) free(x);
	if(y != NULL) free(y);
	if(z != NULL) free(z);
	return false;
}

I think it reduced the possibility of human errors while programming.

cu
Alina

-- 
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser




More information about the Olsr-dev mailing list