本文介绍了如果删除用于释放使用malloc()获得的内存,是否应该产生警告或甚至断言失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,使用 delete 来释放通过 malloc()获得的内存不一定会导致程序



如果 delete 用于释放内存,应该产生警告或者甚至断言失败使用 malloc()



为什么Stroustrup在C ++上没有此功能?

$ b $在C ++中使用delete来释放通过malloc()获得的内存不一定会导致程序爆炸。

p>

不,但它必然导致未定义的行为,这意味着任何事情都可能发生,包括程序启动或程序继续运行看起来是正确的方式。

否。这是困难的,如果不是不可能,在编译时检查。运行时检查很昂贵,而在C ++中,你得不到你不需要支付的费用。



它可能是一个有用的选项在调试时打开,但真的,正确的答案是不要混合和匹配。我从来没有遇到麻烦。


In C++ using delete to free memory obtained with malloc() doesn't necessarily cause a program to blow up.

Should a warning or perhaps even an assertion failure should be produced if delete is used to free memory obtained using malloc()?

Why did Stroustrup not have this feature on C++?

解决方案

No, but it does necessarily result in undefined behavior, which means that anything can happen, including the program blowing up or the program continuing to run in what appears to be a correct manner.

No. This is difficult, if not impossible, to check at compile time. Runtime checks are expensive, and in C++, you don't get what you don't pay for.

It might be a useful option to turn on while debugging, but really, the correct answer is just don't mix and match them. I've not once had trouble with this.

这篇关于如果删除用于释放使用malloc()获得的内存,是否应该产生警告或甚至断言失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:15