rihad wrote:
> On Thu, 06 Nov 2003 12:48:51 +0400, rihad <> wrote:
>
>
>>On Thu, 6 Nov 2003 18:01:06 +1100, "Simon Biber" <> wrote:
>>
>>
>>>double a = malloc(n * sizeof *c);
>>>double b = malloc(n * sizeof *c);
>>>
>>>double c = malloc(n * sizeof *c);
>
>
> "Oh, *pointers* are simple - it's *typing* that's difficult.
"
>
>
>>>if(a && b && c)
>>>{
>>> /* set up a, b */
>>> solve(a, b, c, n);
>>> /* use c */
>>> free(a);
>>> free(b);
>>> free(c);
>>>}
>>
>>What if a failed, you wouldn't be freeing the other guys? I still prefer the
>>"goto to the function trailer" approach.
Yes, that should be a concern which this code does not handle.
However, the code snippet above will not even compile for other
reasons.
>
>
> Namely:
> double a = malloc(n * sizeof *c);
> double b = malloc(n * sizeof *c);
>
> double c = malloc(n * sizeof *c);
Why do you repeat the flawed code?
a, b, c should all be a pointer type.
The variable c is being used before it has been declared.
Namely: A lot of errors in this short snippet of code.
> if(!(a && b && c))
> goto nomem;
> /* set up a, b */
> solve(a, b, c, n);
> /* use c */
>
> nomem:
> free(a);
> free(b);
> free(c);
> }
>
There is no need to do a goto here. Simply free a, b, and c
in the "trailer".
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void solve(double *a, double *b, double *c, double n);
int main(void)
{
double n = 9000.00;
double *a = malloc(sizeof *a);
double *b = malloc(sizeof *b);
double *c = malloc(sizeof *c);
if(a && b && c)
{
solve(a, b, c, n);
printf("*a = %f\n*b = %f\n*c = %f\n",*a,*b,*c);
}
free(a);
free(b);
free(c);
return 0;
}
void solve(double *a, double *b, double *c, double n)
{
*a = log(n);
*b = log10(n);
*c = *a-*b;
printf("log(%.2f) = %f\n",n,*a);
printf("log10(%.2f) = %f\n",n,*b);
printf("log(%.2f) - log10(%.2f) = %f\n\n", *a, *b,
log(*a)-log(*b));
return;
}
--
Al Bowers
Tampa, Fl USA
mailto:
(remove the x to send email)
http://www.geocities.com/abowers822/