Skip to content

Commit 5ecd43c

Browse files
committed
5.4
1 parent bfd6b11 commit 5ecd43c

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#define ALLOCSIZE 10000
2+
3+
static char allocbuf[ALLOCSIZE];
4+
static char *allocp = allocbuf;
5+
6+
char *alloc(int n)
7+
{
8+
if (allocbuf + ALLOCSIZE - allocp >= n) {
9+
allocp += n;
10+
return allocp - n;
11+
} else
12+
return 0;
13+
}
14+
15+
void afree(char *p)
16+
{
17+
if (p >= allocbuf && p < allocbuf + ALLOCSIZE)
18+
allocp = p;
19+
}
20+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
#include <stddef.h>
3+
4+
int main()
5+
{
6+
char *alloc(int);
7+
void afree(char*);
8+
9+
char *p = alloc(10);
10+
if (NULL == p)
11+
return -1;
12+
13+
*p++ = 'H';
14+
*p++ = 'e';
15+
*p++ = 'l';
16+
*p++ = 'l';
17+
*p++ = 'o';
18+
*p++ = '\0';
19+
p -= 6;
20+
21+
printf("%s\n", p);
22+
23+
afree(p);
24+
25+
printf("%s\n", p);
26+
27+
p = alloc(10001);
28+
29+
if (NULL == p)
30+
printf("overflow!\n");
31+
32+
printf("%d\n", NULL);
33+
34+
return 0;
35+
}
36+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdio.h>
2+
3+
int main()
4+
{
5+
int strlen(char*);
6+
7+
char array[100];
8+
char *ptr = "Hello";
9+
10+
int l = strlen("hello, world");
11+
printf("%d\n", l);
12+
13+
l = strlen(array);
14+
printf("%d\n", l);
15+
16+
l = strlen(ptr);
17+
printf("%d\n", l);
18+
}
19+
20+
int strlen(char *s)
21+
{
22+
char *p = s;
23+
24+
while (*p != '\0')
25+
p++;
26+
27+
return p - s;
28+
}
29+

0 commit comments

Comments
 (0)