谁能看出此程序的问题。

时间:2008-05-10 05:02:30   来源:论坛整理  作者:  编辑:chinaitzhe
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



#include <stdio.h>

#include <memory.h>    

#include <stdlib.h>

      main()

      {

        char *p;

        p=(char *)malloc(100);

        if(p)

            printf("Memory Allocated at: %x",p);

        else

            printf("Not Enough Memory!\n");

        p="aaaaaaaaa";

        printf("%s\n",p);

        getchar();

        p=(char *)realloc(p,256);

        if(p)

            printf("Memory Reallocated at: %x",p);

        else

            printf("Not Enough Memory!\n");

        printf("%s\n",p); 

        free(p);

        getchar();

        return 0;

      }


网友回复:VC编译通过
运行结果:Memory Allocated at: 307a8aaaaaaaaa
网友回复:main函数没有返回类型却return 0;
在main前加上int
网友回复:程序的问题大了:
1.
If ptr is a null pointer, the realloc function behaves like the
malloc function for the specified size. Otherwise, if ptr does not match a pointer
earlier returned by the calloc, malloc, or realloc function, or if the space has
been deallocated by a call to the free or realloc function, the behavior is undefined.
2.
The free function causes the space pointed to by ptr to be deallocated, that is, made
available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if
the argument does not match a pointer earlier returned by the calloc, malloc, or
realloc function, or if the space has been deallocated by a call to free or realloc,
the behavior is undefined.

网友回复:问题就出在:
p="aaaaaaaaa";
改为:
strcpy(p,"aaaaaaaaa");
即可。
网友回复:ls de ls:用中文,我真的看不懂你这个英文。
网友回复:mark
网友回复:
引用 4 楼 mymtom 的回复:
问题就出在:
p="aaaaaaaaa";
改为:
strcpy(p,"aaaaaaaaa");
即可。

网友回复:p="aaaaaaaaa";
是将p指向了另外一个地址,并且这个地址是只读的。并且这个地址不在堆上,而是在常量区,realloc会
失败.
同意4楼.
网友回复:
引用 8 楼 szduweibing 的回复:
p="aaaaaaaaa";
是将p指向了另外一个地址,并且这个地址是只读的。并且这个地址不在堆上,而是在常量区,realloc会
失败.
同意4楼.

那请问,p指向“常量区”,是不是叫引用?就是节省内存空间的做法。那什么能确保一定有“aaaaaaaaaaa”这样的数据存在呢?
网友回复:
引用 2 楼 c_spark 的回复:
main函数没有返回类型却return 0;
在main前加上int

没申请成功的话,最好free之后退出程序
网友回复:
引用 9 楼 ONLYBLUEMOON 的回复:
引用 8 楼 szduweibing 的回复:
p="aaaaaaaaa";
是将p指向了另外一个地址,并且这个地址是只读的。并且这个地址不在堆上,而是在常量区,realloc会
失败.
同意4楼.

那请问,p指向“常量区”,是不是叫引用?就是节省内存空间的做法。那什么能确保一定有“aaaaaaaaaaa”这样的数据存在呢?


是否叫引用我不知道,不过的确是节省空间的做法。实际上,就是在常量区有段内存存放了“aaaaaaaaaaa”
那么以后,假如还有其它指针,比如q="aaaaaaaaa";那么也会指向这个地址。
网友回复:
引用 11 楼 szduweibing 的回复:
引用 9 楼 ONLYBLUEMOON 的回复:
引用 8 楼 szduweibing 的回复:
p="aaaaaaaaa";
是将p指向了另外一个地址,并且这个地址是只读的。并且这个地址不在堆上,而是在常量区,realloc会
失败.
同意4楼.

那请问,p指向“常量区”,是不是叫引用?就是节省内存空间的做法。那什么能确保一定有“aaaaaaaaaaa”这样的数据存在呢?


是否叫引用我不知道,不过的确是节省空间的做法。实际上,就是在常量区有段内存存放了…


问个郁闷的,要是没有“aaaaaaaaaaa”,那咱们引用?
网友回复:晕,你不是自己写了 "aaaaaaaaaa" 吗,怎么会没有- -?
网友回复:把这句注释掉就没有关系了
//free(p);

你泄露的内存,系统会帮你回收
^_^
网友回复:
realloc的参数必须是通过malloc之类的函数分配的,
通过malloc分配的空间里存有分配的信息,用于确定分配的大小和回收使用
因为函数需要检查之前分配的大小,
假如新分配的空间大于已经分配的空间,还会回收以前分配的空间


网友回复:
引用 3 楼 mymtom 的回复:
程序的问题大了:
1.
If ptr is a null pointer, the realloc function behaves like the
malloc function for the specified size. Otherwise, if ptr does not match a pointer
earlier returned by the calloc, malloc, or realloc function, or if the space has
been deallocated by a call to the free or realloc function, the behavior is undefined.
2.
The free function causes the space pointed to …

--------------
说明很清楚了,行为将是不确定的。
关键字:看出,程序,问题,

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面