【发布时间】:2014-08-21 06:24:24
【问题描述】:
您好,我在这里新学习,请解释下面的问题,我从很多天开始自己学习,我确实有一些 malloc 函数的问题。请帮助我了解这个网站,它不适合初学者,但我无法找到其他方法来找到解决方案
1) p=malloc(0) // what will it return ?when i calculate size using sideof operator it throw 4 byte?
2) int *p=malloc(4) // when i scan string it throw 0 why sir?
3) *p=2 // while p is store in heap
scanf("%d",*p)//why *p is not possible to scanf here *p why p only?
4) int *p=(int*)malloc(20*sizeof(int))
for(i=0;i<5;i++)
p[i]=i+1;
free(p);
//now after free i am still get same previos value.. why not garbage coz malloc default value is garbage?
5) int main()
{
int *p;
p=(int*)malloc(4);
printf("%d\n",*p); // even here i am getting 0 why nt garbage?
}
谢谢楼主
【问题讨论】:
-
malloc(0)返回的 what 并不重要。您最多可以在返回的地址存储零字节,因此它是不可观察的。您需要知道的是,您可以而且必须最终将结果传递给free。 -
一一提问。
-
“我仍然得到相同的 previos 值.. 为什么不是垃圾......我得到 0 为什么 nt 垃圾?” -- “垃圾”不是一个特定的可识别值。
-
函数不会抛出。他们返回。
-
问题太多……太宽泛了。