1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <assert.h>
struct param
{
double eps;
double delta;
double var;
};
typedef struct
{
char *name;
struct param *param;
}
Distribution;
#define MEMERROR fprintf (stderr,"%s %d : Allocation Memory Error\n", __FILE__,__LINE__)
Distribution *CreateEmptyDistribution (void)
{
Distribution *d = malloc (sizeof *d);
if (d == NULL)
{
MEMERROR;
exit (EXIT_FAILURE);
}
d->name = NULL;
d->param = malloc (sizeof *d->param);
if (d->param == NULL)
{
MEMERROR;
exit (EXIT_FAILURE);
}
return d;
}
void DestroyDistribution (Distribution ** d)
{
free ((*d)->name), (*d)->name = NULL;
free ((*d)->param), (*d)->param = NULL;
free (*d), *d = NULL;
}
int main (void)
{
Distribution *d = CreateEmptyDistribution ();
if (d != NULL)
{
d->name = strdup ("nom");
d->param->eps = 0.1;
d->param->delta = 1e-9;
d->param->var = 1.;
DestroyDistribution (&d);
}
assert (d == NULL);
return 0;
} |
Partager