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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define trace(s) printf ("Error : %s at %s:%d\n", s, __FILE__, __LINE__)
int allocation_type (void *pt, size_t size)
{
int ok = 0;
if (pt != NULL)
{
void **ppt = pt;
if (*ppt == NULL)
{
*ppt = malloc (size);
if (*ppt == NULL)
{
trace ("Allocation memoire");
}
else
{
ok = 1;
}
}
else
{
trace ("Deja alloue");
}
}
else
{
trace ("Pointeur NULL");
}
return ok;
}
int main (void)
{
char *p = NULL;
int ok;
ok = allocation_type (NULL, 10);
if (ok)
{
strcpy (p, "hello");
printf ("'%s'\n", p);
}
ok = allocation_type (&p, 10);
if (ok)
{
strcpy (p, "hello");
printf ("'%s'\n", p);
}
ok = allocation_type (&p, 10);
if (ok)
{
strcpy (p, "hello");
printf ("'%s'\n", p);
}
return 0;
} |
Partager