C_and_CPP 版 (精华区)

发信人: hua (pupa), 信区: C_and_CPP
标  题: [合集] 指针的问题
发信站: 哈工大紫丁香 (Mon May 23 10:52:57 2005), 站内

────────────────────────────────────────
  Ogami (风林火山)                 于  (Fri Aug 27 12:06:36 2004)  说道:

int *p;
p=(int *)malloc(sizeof(int));
free (p);
我觉得free以后p这个指针变量并没有消失,还是指向原来分配给它的那块内存,只是那块
内存现在可以由系统自由支配了,比如分配给其他变量。不知对否?


────────────────────────────────────────
  iamxiaohan (潇寒·油麻菜籽)      于  (Fri Aug 27 12:08:41 2004)  说道:

nod
【 在 Ogami (风林火山) 的大作中提到: 】
: int *p;
: p=(int *)malloc(2);
: free p;
: ...................



────────────────────────────────────────
  IkEbAnA (插.花)                  于  (Fri Aug 27 13:39:02 2004)  说道:

可是你看

#include <stdio.h>
#include <stdlib.h>

int main()
{
        int* p = (int*)malloc(sizeof(int)*2);
        *p = 1;
        *(p+1) = 2;
        printf("%d  %d\n",*p,*(p+1));
        free(p);
        printf("%d  %d\n",*p,*(p+1));
        
        exit(0);
}

MinGW

1  2
0  2            // 为啥第一个是 0


Terminated with return code 0
Press any key to continue ...


【 在 iamxiaohan (潇寒·油麻菜籽) 的大作中提到: 】
: nod




────────────────────────────────────────
  dairyfeel (将惆怅变得愉快舒畅)   于  (Fri Aug 27 15:40:24 2004)  说道:

个人认为,释放指针后,指针指向的数和原来的数没有关系了
【 在 IkEbAnA (插.花) 的大作中提到: 】
: 可是你看
: #include <stdio.h>
: #include <stdlib.h>
: int main()
: {
:         int* p = (int*)malloc(sizeof(int)*2);
:         *p = 1;
:         *(p+1) = 2;
: ...................





────────────────────────────────────────
  iamxiaohan (潇寒·油麻菜籽)      于  (Fri Aug 27 18:14:17 2004)  说道:

free之后,这块内存怎么使用是操作系统的事,
它的值是未定的,在不同的情况下,可能不同
【 在 IkEbAnA (插.花) 的大作中提到: 】
: 可是你看
: #include <stdio.h>
: #include <stdlib.h>
: ...................



────────────────────────────────────────
  Ogami (风林火山)                 于  (Fri Aug 27 18:32:26 2004)  说道:

我试了一下,输出两个-572662307,释放p把两个int占的内存都归还系统了吗?
【 在 IkEbAnA (插.花) 的大作中提到: 】
: 可是你看
: #include <stdio.h>
: #include <stdlib.h>
: int main()
: {
:         int* p = (int*)malloc(sizeof(int)*2);
:         *p = 1;
:         *(p+1) = 2;
: ...................





[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.881毫秒