English 中文(简体)
删除 MyClass * 阵列[10]
原标题:delete MyClass *Array[10]

我真的被困在试图用删除来处理内存 而不让它被循环。

    MyClass *Array[10];

cout << "Step 1 - Allocation" << endl << endl;
Array[0] = new MyClass();
Array[1] = new MyClass();
Array[2] = new MyClass(2, 4.6);
Array[3] = new MyClass(*Array[2]);
Array[4] = new MyClass();
Array[5] = new MyClass(13, 66.6);
Array[6] = new MyClass(75, 9.43);
Array[7] = new MyClass(*Array[6]);
Array[8] = new MyClass(*Array[1]);
Array[9] = new MyClass(*Array[3]);

cout << endl << "Step 2 - Write" << endl << endl;
for(int i=0; i<10; i++)
{
    Array[i]->write();
    cout << endl;
}

cout << endl << "Step 3 - Deallocation" << endl << endl;

我尝试删除 [] 数组, 但无效 。

代码必须保持原样, 因为它按照要求要做什么是正确的 。 唯一要做的就是添加删除( 单行而非环行) 来删除矩阵 。

最佳回答

您需要删除您使用 new 创建的每个对象 :

for (unsigned i(0); i < 10; ++i)
    delete Array[i];

Array 本身是一个本地变量;您没有使用 new 来创建它,所以您没有使用 delete 来销毁它。 当它超出范围时, 它将被销毁 。

为了解决这项删除必须“单行而不是循环”这一最新限制,我想可以写:

delete Array[0]; delete Array[1]; delete Array[2]; delete Array[3]; delete Array[4]; delete Array[5]; delete Array[6]; delete Array[7]; delete Array[8]; delete Array[9];

这是单行代码, 不使用循环。 无法使用一个 < code > delete 表达式来摧毁 10 个实际上无关的物体 。


但不要这样做。 真的。 手动资源管理非常危险, 很难正确操作 。 C++ 具有自动资源管理能力, 您应该使用这些能力 。 在这种情况下, 最好使用 < code>std:: victor< MyClass> :

std::vector<MyClass> Array;
Array.push_back(MyClass());
Array.push_back(MyClass());
Array.push_back(MyClass(2, 4.6));
Array.push_back(MyClass(Array[2]));
// Etc.

在此示例中不存在 new , 因此不需要 delete std:: victor 将在销毁时清理一切 。

问题回答

Operator delete[] must be used in conjunction with operator new[], which you haven t used here. You manually new ed every single instance of MyClass. You have to manually delete every single instance of MyClass.

delete[] 被用来删除由 new[] 分配的东西,它不为数组的每个元素调用 delete 。您必须在循环中调用您每个数组元素的 delete

std::vector<std::unique_ptr<MyClass>> Array;
Array.resize(10);

无需删除。

我真的被困在试图用删除来处理内存 而不让它被循环。

如果您不手动分配内存, 无需手动处理内存 。

    MyClass Array[10] = {
        MyClass(),
        MyClass(),
        MyClass(2, 4.6),
        MyClass(Array[2]),
        MyClass(),
        MyClass(13, 66.6),
        MyClass(75, 9.43),
        MyClass(Array[6]),
        MyClass(Array[1]),
        MyClass(Array[3])
    };

    // there s nothing to delete now

new delete 是具有某些特殊用途的高级 C++ 特性。 不需要时不要使用这些特性 。

您可以使用“ a href=” 这样的删除器真菌“ http://www.codeproject. com/ articles/6485/ A- Functor- that- deletes-pointers- from- STL- securance” rel=“ nofollow” > this one 并做一些类似的事情:

for_each(Array.begin(), Array.end(), free_ptr<C>());

我唯一能看到一行中清理的方法就是重写它,使阵列能够指向一列天体,而不是指向一列指针。

MyClass *Array = nullptr;

cout << "Step 1 - Allocation" << endl << endl;
Array = new MyClass[10];
Array[0] = MyClass(); //removed "new"
...

cout << endl << "Step 2 - Write" << endl << endl;
for(int i=0; i<10; i++)
{
    Array[i].write(); //Objects aren t pointers anymore
    cout << endl;
}

cout << endl << "Step 3 - Deallocation" << endl << endl;
delete[] MyClass;




相关问题
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?

热门标签