#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int * ptr;
printf("before malloc pointer is :%p \n",ptr);
printf("before malloc valu is :%d \n",*ptr);
ptr = malloc(sizeof(int));
printf("after malloc pointer is %p \n",ptr);
printf("after malloc valu is :%d \n",*ptr);
int jig=32;
*ptr = jig;
printf("after assignment valu is : %d\n",*ptr);
free(ptr);
printf("after free %p \n",ptr); // after free pointer holds sane address then
printf("after fee is %d\n",*ptr); // why it coudnt print that???
return 0;
}
出力は次のようになります。フリー()の仕組みは?
before malloc pointer is :0x6edff4
before malloc valu is :7265660
after malloc pointer is 0x9a52008
after malloc valu is :0
after assignment valu is : 32
after free 0x9a52008
after fee is 0
我々はそのメモリの値を印刷することはできませんなぜ無料まだポインタは、そのメモリのアドレスを保持した後??。
無料()は何ですか?
すべてのメモリを0にするだけですか?
のprintfで「のmalloc VALUの前に」、あなたはNULLポインタを参照解除されています。それをしないでください。 –
ポインタの宣言の後にNULLが指していない...それは安全です。 –
いいえ、それは標準化されていないので、必ずしも安全ではありません。さらに、NULLの場合でもそれを尊重してはなりません。それはまともなシステム上のsegフォルトにつながります。また、今後はキャップロックの使用を控えてください。 –