English 中文(简体)
C++:是推回(新目标)?
原标题:C++: is push_back(new Object()) a memory leak?

下述C++编码是传记吗?

list.push_back(new String("hi"));

我的理解是,从任何固定的收集/集装箱中推回,总是提供复制件。 因此,如果新扼杀被复制,永远不会删除新的扼杀权? 由于在推回后没有提及它......

我在这里纠正或错误?

感谢。

Jbu

编辑:我认为我错了,因为新兵将回来......我们总是有点人能够删除新的强权。

最佳回答

无,病媒储存点和复印件由点人提供。 您可在以后删除该物体。

(如果说谎,你可能会泄露出来,而你不会赶上并妥善处理。) 因此,你可以考虑使用智能点。

问题回答

是的,但不是因为你认为的原因。

视<代码><>list>/code>的界定和初始化程度而定,push_back可放弃一个例外情况。 如果是,点人从<代码>新返回,永远不会获释。

但是,如果选择<代码>push_back > 回归,则将一份由<代码>new退回的点码复印件加以储存,因此,如果你do <><>>>>>>>>>> 代码>>适当,我们就可以在后面打上<代码>delete。

如果我看到这部法典,我会非常怀疑有可能泄露记忆。 在表面上,似乎将分配的<代码>String*添入list<String*>。 在我的经验中,往往采用错误处理法,不能适当地解放所分配的记忆。

虽然这种危险在许多情况下不一定是记忆泄露。 考虑以下例子:

class Container {
  ~Container() {
    std::list<String*>::iterator it = list.begin();
    while (it != list.end()) {
      delete *it;
      it++;
    }
  }

  void SomeMethod() {
    ...
    list.push_back(new String("hi"));
  }

  std::list<String*> list;
}

该法典没有漏水,因为含蓄电池组负责所分配的记忆,并将在反应堆中释放。

http://www.ohchr.org。

正如主持人所指出的,如果<代码>push_back,则仍然有泄漏。 方法有例外。

你是正确的,只要从名单上删除,就不去掉脚。

是的,这是不记名的漏.,你会采取一些步骤,删除其中的点子。

And the best way to accomplish that is to use a smart pointer. For example, Boost s shared_ptr or C++0x s shared_ptr.

list.push_back(new String("hi"));

为什么你们首先分配动态的扼杀? 除非你想通过改变扼杀(非常罕见)的方式在你方案的不同部分之间沟通,否则:

std::list<std::string> list;         // note: no pointer!
list.push_back(std::string("hi"));   // explicitly create temporary
list.push_back("hi");                // alternative: rely on coercion

页: 1

您可以删除该物体:

delete list[i];
list.erase(list.begin() + i);

或以下列方式列出整个清单:

for (unsigned int i = 0; i < list.size(); ++i)
{
  delete list[i];
}
list.clear();




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签