简体   繁体   English

C ++新的运算符用法问题

[英]c++ new operator usage issue

I just accidentally write the code below. 我只是不小心写了下面的代码。 It is compiled using gcc 4.4.7 in linux environment. 它是在Linux环境中使用gcc 4.4.7编译的。

int main ()
{
        new int;
        return 0;
}

I am surprised the compiler does not indicate any error or warning. 我很惊讶编译器没有指出任何错误或警告。 Is the c++ standard mentioned about this? 是否提到了c ++标准? Is it still possible to prevent memory leak in this situation? 在这种情况下是否仍然可以防止内存泄漏? Any advice is welcome. 欢迎任何建议。

There is no problem with this. 这没有问题。 This is valid in c++. 这在c ++中有效。

This is perfectly valid C++. 这是完全有效的C ++。 Why are you surprised that it compiled? 为什么对它进行编译感到惊讶?

To prevent memory leaks you should use shared_ptr instead of raw pointers: 为了防止内存泄漏,您应该使用shared_ptr而不是原始指针:

#include <memory>
int main ()
{
        std::shared_ptr<int> i(new int);
        return 0;
}

Now the new allocated object is deleted at the end of the scope. 现在,新分配的对象在作用域末尾被删除。 And you do not have a memory leak in you code. 而且您的代码中没有内存泄漏。 For more details have a look at the dynamic memory management of C++11 Dynamic Memory management 有关更多详细信息,请查看C ++ 11 动态内存管理中的动态内存管理。

This is one reason why you should try to avoid raw "new" as much as possible in new code. 这就是为什么您应尽量避免在新代码中使用原始的“新”的原因之一。 std::make_shared and in c++14 std::make_unique are much safer as they will ensure that memory gets deleted properly by returning shared_ptr and unique_ptr objects that know when and how to delete the object. std :: make_shared和c ++ 14中的std :: make_unique安全得多,因为它们将通过返回知道何时以及如何删除该对象的shared_ptr和unique_ptr对象来确保正确删除内存。 The intention is that raw new will mostly only be needed in low level code implementing data structures. 目的是仅在实现数据结构的低级代码中仅需要原始数据。

As I commented, a program can "legitimately" leak memory. 如我所评论,程序可以“合法地”泄漏内存。

On most operating systems (notably Posix or Linux), the kernel will release all the memory used by a process after the process exits . 在大多数操作系统(尤其是Posix或Linux)上,内核将在进程退出后释放该进程使用的所有内存。

So if during its initialization, a program allocates some (heap) data -in limited quantity- and don't bother releasing it at all, it is a "legitimate" memory leak (and may real programs exhibit that behavior: eg the GCC compiler, or Firefox browser, or most X11 client libraries, etc...). 因此,如果程序在初始化期间分配了一些(堆)数据(数量有限)并且根本不打算释放它,那么这就是“合法”的内存泄漏(并且实际程序可能会表现出这种行为:例如,GCC编译器,或Firefox浏览器或大多数X11客户端库等)。

However, leaks which happen continuously during the normal operation of the program and which increase the memory consumption are frowned upon. 但是,对于在程序正常运行期间连续发生的泄漏和增加内存消耗的泄漏,人们不予理会。

Also, I believe that it can be proven that static analysis of memory leaks is equivalent to the halting problem so there is no way to always detect it at compile time: either you'll get some false alarms , or some leaks will stay undetected. 另外,我相信可以证明,对内存泄漏的静态分析等同于暂停问题,因此无法始终在编译时对其进行检测:您将收到一些错误警报 ,或者某些泄漏将无法检测到。

At runtime, you could use valgrind to chase memory leaks. 在运行时,您可以使用valgrind追逐内存泄漏。

Also, the liveness of some memory zone is a global property of the program. 同样,某些存储区的活动性是程序的全局属性。 Read more about garbage collection and perhaps consider using Boehm's conservative GC . 阅读有关垃圾收集的更多信息,或者考虑使用Boehm的保守GC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM