为什么free不能清除指针值? [英] Why does free not clear out a pointers value?

查看:76
本文介绍了为什么free不能清除指针值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图向朋友解释免费的工作原理,所以我想了以下代码示例以尝试对其进行解释(我知道很复杂):

I was trying to explain how free works to a friend, so I thought up the following code example to try and explain it (Complex, I know):

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

int main()
{

  char *stringPtr = NULL;

  //Pass the size of the string in bytes to malloc
  stringPtr = (char *)malloc(10 * sizeof(char));

  strcpy(stringPtr, "A String. ");
  printf("%s", stringPtr);
  free(stringPtr);

  printf("%s", stringPtr);

  return 0;
}

考虑到此代码将释放分配在堆上的内存,我假设如果我随后打印出分配给指针的字符串,则它将产生奇怪的结果或程序崩溃,因为我认为会尝试输出一个空值;但是,当我运行它时,它最终只输出了两次字符串.我想我会逐步了解它的原因,因为在我看来,也许它将字符串复制到堆栈或其他内容中,然后指针将指向一个新的内存地址,而旧的内存毕竟被清理了(并不是那种情况会有意义,但是,嘿,我不知道那还会是什么),但是当我单步执行代码时,不仅没有释放内存,指针实际上仍然指向相同的位置.内存地址具有相同的值.

Thinking that this code would free up the memory allocated on the heap, I assumed that if I then went to print out the string that was assigned to the pointer, it would then do something weird or crash the program since I assumed it would try to output a null value; however when I ran it, it ended up just outputting the string twice. I figured I'd step through it to see why, because in my mind I thought maybe it copied the string to the stack or something and the pointer would then be pointing to a new memory address and the old memory got cleaned up after all (not that that scenario would make any sense, but hey, I didn't know what else it would be), but when I stepped through the code not only did it not free up the memory, the pointer actually was still pointing to the same memory address and it had that same value.

当我查看它时,人们说在调用free之后,您需要为指针分配一个空值,但是我的问题是为什么呢?不应该只释放该内存并将指针设置为null吗?保留价值有什么意义?还是我只是不了解某事?

As I looked into it people said that you need to assign the pointer a null value after calling free, but my question is why? Shouldn't free just deallocate that memory and set the pointer to null? What's the point in preserving the value? Or am I just not understanding something?

推荐答案

不应该释放...释放该内存

Shouldn't free ... deallocate that memory

假设您的公司决定移动您的工作区,因此他们为您分配了一张新办公桌,并撤消了您现有的办公桌.他们会立即清理您的旧办公桌吗?可能不是,除非他们立即需要它.如果您回去检查抽屉,您可能会发现您留在那儿的口香糖,或者可能没有.您的空间已已分配,但这并不意味着它会立即清除消失.不再是你的空间了.

Suppose your company decides to move your workspace, so they allocate you a new desk and deallocate your existing one. Are they going to clear out your old desk immediately? Probably not, unless they need it immediately. If you go back and check the drawers, you might find that gum you left there, or you might not. Your space was deallocated, but that doesn't mean it's immediately cleared or gone. It's just not your space any more.

不应该释放...将指针设置为空吗?

Shouldn't free ... set the pointer to null?

free获取指针的副本,因为指针是按值传递的. free无法擦除保存指针的变量.

free gets a copy of your pointer, since the pointer is passed by value. free can't wipe the variable you were holding the pointer in.

这篇关于为什么free不能清除指针值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆