1 #define ALLOCSIZE 10000 /* 可用空间大小 */
2
3 static char allocbuf[ALLOCSIZE];/* alloc使用的存储区 */
4 static char *allocp = allocbuf; /* 下一个空闲位置 */
5
6 char *alloc(int n) /* 返回指向n个字符的指针 */
7 {
8 if (allocbuf + ALLOCSIZE - allocp >= n) { /* 有足够的空闲空间 */
9 allocp += n;
10 return allocp - n; /* 分配前的指针 */
11 }
12 else /* 空间不够 */
13 return 0;
14 }
15
16 void afree(char *p) /* 释放p指向的存储区 */
17 {
18 if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
19 allocp = p;
20 }
21
22 /*
23 指针运算
24 2019-05-09
25 */
26 void Pointer_cal()
27 {
28 char *allAddr;
29 printf("空闲地址:%d\r\n", allocp);
30 allAddr = alloc(100);
31 printf("空闲地址:%d\r\n", allocp);
32 afree(allAddr);
33 printf("空闲地址:%d\r\n", allocp);
34 }