释放字符串后仍然可以打印字符串吗? [英] Can still print a string after I freed it?

查看:94
本文介绍了释放字符串后仍然可以打印字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习和测试C语言中的内存分配,我想测试如果调用free()会发生什么.

I am learning and testing memory allocation in C and I want to test what happens if free() is called.

我运行下面的程序后,可能会出现分段错误或指针为NULL.但是,我仍然可以像在Output中一样成功地打印字符串.我还尝试两次释放str,然后发生错误,输出2.

I expected there could be a segmentation fault or pointer is NULL after I run the program below. However, I can still successfully print the string as in Output. I also tried to free str twice, then an error as Output 2 occurred.

似乎先前分配的内存已成功释放,但内存中的数据未清除.那是对的吗?如果是这样的话,程序什么时候可以清理那些释放的内存空间?如果数据被释放但没有清理,那安全吗?

It seems the previously allocated memory is successfully deallocated, but the data on the memory is not cleaned up. Is that correct? If that is the case, when will the program clean up those deallocated memory space? Is that safe if the data is deallocated but not cleaned up?

回答以上任何问题将有帮助!谢谢!

Answers to any question above will be helpful! Thanks!

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

int main()
{
    printf("Hello, World!\n");
    char *str = (char *)malloc(24);
    char *str2 = "tutorialspoint";
    strcpy(str, str2);
    free(str);
    printf("[str] %s\n", str);
    return 0;
}

输出

Hello, World!
[str] tutorialspoint

输出2

main(83218,0x7fff9d2aa3c0) malloc: *** error for object 0x7fb0b2d00000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

编辑

谢谢大家的有益答复.现在,我了解到C语言中存在一些未定义的行为(UB),这有助于我理解之前让我感到困惑的其他事情,例如,写入超出allocate范围的字符串(例如下面的代码片段中的内容).根据 wiki ,这导致了UB,但是该程序不会崩溃.

Edit

Thank you all for helpful replies. Now I understand that there are some undefined behaviors (UB) in C and this helped me understand something else confused me before such as writing to a string beyond the scope of allocated(like what's in the code snippet below). This caused UB according to wiki, but the program will not crash.

如果我弄错了,请随时纠正我!

Feel free to correct me if I got it wrong!

char *str = (char *)malloc(0);
char *str2 = "tutorialspoint";
strcat(str, str2);
printf("[str] %s, [addr] %p\n", str, str);

推荐答案

您需要学习未定义行为的概念.

即使您在释放字符串后打印字符串并且它"有效",也并不意味着它有效.这只是当行为被称为未定义时可能发生的事情,在这种情况下就是这种情况.

Even if you print the string after you free it and it "works", it doesn't mean it worked. It's just one of the things that can happen when the behavior is said to be undefined, which in this case it is.

此外,除非您执行此操作,否则指针将不会是NULL

Also, the pointer will not be NULL except if you do this

free(ptr);
ptr = NULL;

free(),也没有将所有字节都设置为0.

and free(), doesn't set all bytes to 0 either.

free()函数的功能恰如其名.它允许malloc()再次使用free() d块.指针指向的数据可能仍保持不变,这就是为什么您仍可以在free()之后打印以前的内容的原因.但是绝对不能保证如果您在free()之后取消对指针的引用,或者该数据是否仍然存在,是否被完全或部分覆盖,将会发生什么情况.

The free() function just does exactly what it's name suggests. It allows malloc() to use the free()d chunk again. The data the pointer pointed to might still be there unchanged and that is the reason why you can still print the previous content after free(). But there is absolutely no guarantee of what will happen if you do dereference a pointer after free() or whether the data still exists or, was completely or partially overwritten.

如果在free() d后确实取消了对指针的引用,那么您确实知道一件事,即行为是不确定的.

If you do dereference the pointer after having free()d it, there is one thing you do know, the behavior is undefined.

这篇关于释放字符串后仍然可以打印字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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