In article <3F*************@yahoo.com>, CBFalconer wrote:
Complications not needed. The equivalent is:
....
x = y = z = NULL
if (!(x = malloc(a))) printf("...");
else if (!(y = malloc(b))) printf("...");
else if (!(z = malloc(c))) printf("...");
else {
....
}
free(z); free(y); free(x);
and you can always factor "malloc(b)" etc. into a routine:
T *routine(b, others)
{
....
return malloc(b);
}
if you must have independent operations between the mallocs.
This is also an interesting place for a "," (comma) operator.
x = y = z = NULL
if ( !(x = malloc(a))) printf("...");
else if (setup1(x), !(y = malloc(b))) printf("...");
else if (setup2(x,y),!(z = malloc(c))) printf("...");
else {
....
}
where setup1() and setup2() can be either true functions,
or just some simple in-line code.
It doesn't use malloc(), but you can see a similar interaction
of the comma operator and a chain of else-if's in busybox's
date.c:date_conv_ftime() routine. While a bit unusual,
it doesn't take long to grasp, and the code path is greatly
simplified compared to other constructs. This is the author
speaking, so you can take this as a totally unbiased assessment.
- Larry