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
|
/* malloc example:*/
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand */
//////////////////////////////////////////////
unsigned char mehdi[24] =
{
0x55, 0xAA, 0x02, 0x01, 0x03, 0x02, 0xc0, 0xd0,
0x0e, 0x07, 0x60, 0x90, 0xf0, 0x40, 0x50, 0xa0,
0xf0, 0xc0, 0xb0, 0x04, 0x80, 0xd0, 0x02, 0x01
};
int main()
{
int i, n;
char *buffer;
unsigned char *man;
man = (unsigned char *) malloc(64);
if (man == NULL)
{
fprintf(stderr, "Out of memory");
return -1;
}
printf("/////////////////////////////befor "
"free()/////////////////////////////////////\n");
for (int i = 0; i < 24; i++)
man[i] = mehdi[i];
man[24] = '\0';
for (i = 0; i < 24; i++) {
printf("befor free man[%d]==0x%x\n", i, man[i]);
}
free(man);
printf("/////////////////////////////after "
"free()/////////////////////////////////////\n");
/* CAUTION - accessing memory after free is undefined behaviour */
for (i = 0; i < 24; i++) {
printf("after free man[%d]==0x%x\n", i, man[i]);
}
printf("\nall data of pointer ereased!!");
return 0;
}
|