- 论坛徽章:
- 0
|
调用exit的时候用malloc分配的内存会自动释放吗?
- [zdai@localhost test]$ more exit_free.c
- #include<stdio.h>;
- #include<unistd.h>;
- #include<stdlib.h>;
- void fun( int i);
- int *p;
- void fun( int i)
- {
- // int *p;
- p = malloc(sizeof(int));
- *p = i;
- printf("malloc successful,and %d has been store to the memory!\n",i);
- printf("malloc the address is %x\n",p);
- //exit (2);
- }
- int main(void)
- {
- pid_t pid;
- int k;
- int status;
- k = fork();
- if ( k == 0)
- {
- fun(1);
- exit(5);
- }
- else {
- sleep(1);
- printf("------\n");
- pid = wait(&status);
- printf("%x\n",p);
- printf("%d\n",*p);
- return 0;
- }
- }
- [zdai@localhost test]$
复制代码
随便写的,大家不要见效.这样不知道算不算验证
结果是- [zdai@localhost test]$ ./exit_free
- malloc successful,and 1 has been store to the memory!
- malloc the address is 80498a8
- ------
- 0
- Segmentation fault
- [zdai@localhost test]$
复制代码
子进程中malloc的空间在exit之后,好像是没了. |
|