|
|
发表于 2006-2-8 10:27:12
|
显示全部楼层
已经释放了
释放 不等于 把整个那块区域以0填充
目前glibc的实现只是把前4个字节弄成0
试试这个
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main ()
- {
- char *a = NULL;
- char *b = NULL;
- a = strdup ("hello world !");
- b = strtok (a, " ");
- printf ("a=%s\n", a);
- printf ("b=%s\n", b);
- printf ("\n");
- b = strtok (NULL, " ");
- printf ("a=%s\n", a);
- printf ("b=%s\n", b);
- printf ("\n");
- //free(b);
- free(a);
- printf ("a=%s\n", a);
- printf ("b=%s\n", b);
- printf ("\n");
- a = (char *)malloc(sizeof("hello world !") + 1);
- printf("now malloc an buffer whose size is equal to "hello world !"\n"
- "this buffer is just the buffer which strdup previously returned\n"
- "which means strdup returned buffer is reused, coz it's been freed\n");
- strcpy(a, "hello earth !");
- printf("now copy "earth" to where "world" originally resides\n");
- printf ("and b becomes: %s\n", b);
- }
复制代码 |
|