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 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| /* aloc.c K&R SE page 101*/
/*
* x86_64-pc-cygwin-gcc-7.3.0.exe -Wall -Werror -pedantic -ansi -m64 -std=c99 aloc.c -o aloc.exe
*
* Elle demande au compilateur dutiliser le standard c99,
* par défaut gcc compile en gnu89 qui est simplement du c89 avec quelque extensions comme les commentaires à la C++,
* la définition de variable au milieu du code ou encore les « compound litterals », etc
*
* 2.0 to the power of 64.0 is 18446744073709551616.0
* LLONG_MAX n = 9223372036854775807
* sizeof *alloc=8
* *allocp=0
* allocp=4299190272 n=10 allocp=4299190282 pointeur sur=4299190272
* allocp=4299190282 n=20 allocp=4299190302 pointeur sur=4299190282
* allocp=4299190302 n=30 allocp=4299190332 pointeur sur=4299190302
* allocp=4299190332 n=40 allocp=4299190372 pointeur sur=4299190332
*/
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <math.h>
#define ALLOCSIZE 100 /* size of available space */
static char allocbuf [ALLOCSIZE]; /* storage for alloc */
static char *allocp = allocbuf; /* //next free position -std=c99 */
char *alloc(int n) /* return pointer to n charracters */
{
if (allocbuf + ALLOCSIZE - allocp >= n) /* it fits */
{
printf("\nallocp=%"PRId64"\tn=%d\t", (uint64_t)allocp, n);
allocp += n;
printf("allocp=%"PRId64"\t", (uint64_t)allocp );
return allocp - n; /* old p */
}
else /* not enough room */
{
return 0;
}
}
int main(int argc, char *argv[])
{
int i = 0;
uint64_t retournee;
uint64_t n = LLONG_MAX;
double x = 2.0, y = 64.0, z;
z = pow( x, y );
printf( "\n%.1f to the power of %.1f is %.1f\n", x, y, z );
printf ("LLONG_MAX n = %"PRId64"\n", n);
printf("sizeof *alloc=%"PRId64" \n", sizeof (char *));
printf("*allocp=%"PRId64" \n", (uint64_t)*allocp);
while ((i += 10) < ALLOCSIZE)
{
retournee = (uint64_t) alloc(i);
if(retournee != 0)
{
printf("pointeur sur=%"PRId64" \n", retournee);
}
/* system("pause"); */
}
return 0;
}
void afree(char *p) /* free storage pointed to by p */
{
if(p >= allocbuf && p < allocbuf + ALLOCSIZE)
{
allocp = p;
}
} |
Partager