简体   繁体   English

新的和删除的经验法则

[英]rule of thumb for new and delete

I was wondering what the rule of thumb for new and delete is. 我想知道newdelete的经验法则是什么。 I always thought that every time I call new , I should have a delete . 我一直以为,每次调用new ,都应该delete

In the case below, if I include the destructor however, I get a bad excess error. 在以下情况下,如果我包含destructor ,则会收到严重的过量错误。 If I don't include the destructor , my code works fine. 如果我不包含destructor ,则我的代码可以正常工作。

struct Foo
 {
     Foo(int A, int B)
     {
          bar = new std::vector< std::vector < int > >(A, std::vector<int>(B,2);
          //creates a vector of A vectors where each nested vector contains the number 2 B times. 
     }

     ~Foo() //Get bad access error if destructor included in code. 
     {
          delete[] bar;
     }     

     std::vector< std::vector < int > > *bar;
 };

int main()
{
    Foo X;

    return 0;
}

It should be delete bar; 应该是delete bar; not delete [] bar; delete [] bar;

Add another rule to your list of rules. 将另一条规则添加到您的规则列表。

The delete line should have a [] only if the new line has a [some number] 仅当新行具有[some number]时,删除行才应具有[]。

In your case the new line does not have a [some number] 您的情况下, new行没有[some number]

 bar = new std::vector< std::vector < int > >(A, std::vector<int>(B,2);

So your delete line also should not have one. 因此,您的删除行也不应包含一个。

The above class might be better written as: 上面的类可能写得更好:

struct Foo   
 {
     Foo(int A, int B)
       :  bar(A, std::vector<int>(B,2)) //creates a vector of A vectors where each nested vector contains the number 2 B times. 
     {}
     // Default copy
     // Default destructor.

     std::vector< std::vector < int > > bar;
 };

This avoids explicit new and delete ; 这避免了显式的newdelete often the best rule of thumb for using them correctly! 通常是正确使用它们的最佳rule of thumb

With unmanaged code like c++ the rule of thumb is whatever is required by your design. 对于像c ++这样的非托管代码,经验法则是您设计所需要的。

In general, it's a good idea to clean up your objects to avoid memory leaks, but you could have a situation where you want to keep something in memory. 通常,清理对象以避免内存泄漏是一个好主意,但是您可能会希望将某些内容保留在内存中。

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

相关问题 C ++构造函数重载的经验法则? - Rule of thumb for C++ constructor overloads? 使用/ O2与/ Ox进行编译 - 这是更快的(根据经验)? - Compiling with /O2 versus /Ox — which is faster (as a rule of thumb)? c ++ Jobqueue:工作线程的数量是否有经验法则? - c++ Jobqueue: Is there a rule of thumb for the number of worker threads? 什么时候返回指针和返回对象的一般经验法则? - General rule of thumb when to return Pointer vs return Object? 最简单的经验法则是避免违反严格别名规则? - Simplest rule of thumb to avoid breaking strict-aliasing rules? concurrency :: parallel_sort的开销和性能影响(经验法则)? - concurrency::parallel_sort overhead and performance hit (rule of thumb)? 通过值时的经验法则比通过const引用更快吗? - Rule of thumb for when passing by value is faster than passing by const reference? 标准库/模板化容器的常量语义的经验法则? - Rule-of-thumb for const semantics for standard library/templated containers? 是否有一个经验法则来决定在哪里存储我的类(即什么类型的内存)? - Is there a rule of thumb to decide where to store my classes (i.e. what type of memory)? “根据经验,在C ++中使所有方法成为虚拟” - 声音建议? - “As a rule of thumb, make all your methods virtual” in C++ - sound advice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM