使用析构函数是只删除动态分配的数组还是所有数组?
Using destructor is for deleting only dynamically allocated arrays or all arrays?
我是 C++ 的新手,所以我在内存管理方面苦苦挣扎。
我知道当我们有一个指向数组的指针时,这意味着它是动态创建的,所以我应该稍后在我的析构函数中删除它。但是如果我们有一个普通数组怎么办,我是否也需要在这个普通数组的析构函数中包含一个 delete 语句?或者程序会自动执行?
例如,我有一个像这样的普通数组
int myArray[];
在我的析构函数中,我应该包括这个:
delete[] myArray;
?
提前致谢,
But what if we have a normal array, do I need to include a delete statement in the destructor for this normal array too? or the program will do that automatically? for example, I have a normal array like this int myArray[];
and in my destructor, should I include this: delete[] myArray;
?
没有。如果您没有 new[]
数组,则不需要 delete[]
它。其他一切都是自动完成的。这就是为什么使用 std::array
、std::vector
等非常棒:使用它们时无需写 new[]
,因此您不必担心您可能会忘记一个delete[]
.
"I know that when we have a pointer to an array, it means it is dynamically created"
这是不正确的。您基本上可以使用指向任何内容的指针,包括堆栈变量。动态分配通常会给你一个指针(这可能是混淆的根源)但并不是每个指针都来自动态分配。考虑一下:
void foo()
{
int a;
int* b = &a; // No dynamic allocation.
}
同样,这里既然没有new
,就不会有delete
。
I know that when we have a pointer to an array, it means it is dynamically created
没有。这种归纳是不正确的。指针也可以指向非动态创建的数组。
so I should delete it later in my destructor.
如果您的 class 实例负责该数组的销毁,那么析构函数确实是一个可能应该完成的地方。
do I need to include a delete statement in the destructor for this normal array too? for example, I have a normal array like this int myArray[];
没有。只有使用 new
创建的对象才会使用 delete
销毁。只有用 new[]
创建的对象才会用 delete[]
销毁。没有别的。
or the program will do that automatically?
是的。
具有自动存储持续时间的对象在声明它们的范围结束时自动销毁。这就是存储名称 class 的来源。具有静态存储持续时间的对象在 main
returns 后销毁。临时对象在完整表达式结束时被销毁(除非通过将对象绑定到引用来延长它们的生命周期)。当超级对象(在此上下文中不表示基 class)对象被销毁时,成员变量也被销毁。
只有动态创建的对象必须手动销毁。
虽然了解它是如何完成的很有用,但很少需要在 C++ 中进行手动内存管理。标准库提供的容器可以为您完成繁重的工作。人们通常会使用 std::vector
.
而不是动态创建数组
内存管理对任何人来说都不是一件容易的事,尤其是对于那些刚接触 C++ 的人。我建议查看术语 stack
和 heap
,希望它们能澄清一些事情。
I know that when we have a pointer to an array, it means it is dynamically created, so I should delete it later in my destructor.
这不完全正确,指针只是一个变量,用于保存某物的地址(变量所在的位置)。您可以像这样创建指向任何变量的指针:
int a = 5;
int *aPtr = &a;
这并不意味着 a
或 aPtr
是动态的。
然而,这样创建的指针将是动态的,需要删除:
int *aPtr = new int(5);
这里最大的区别是 new
关键字。 new
生成动态内存(放入堆中的内容)。每次使用new
关键字,都要用delete
关键字与之匹配。
But what if we have a normal array, do I need to include a delete statement in the destructor for this normal array too? or the program will do that automatically? for example, I have a normal array like this int myArray[];
and in my destructor, should I include this: delete[] myArray;
?
不,您不需要调用 delete[] myArray;
因为这个数组将在堆栈上生成。我的意思是,当您 return 退出创建该数组的函数时,该数组将被清除出内存。
手动管理内存的另一种方法是使用 std::array
、std::vector
,甚至 std::unique_ptr
或 std::shared_ptr
等智能指针,它们会在对它的引用不见了。
希望对您有所帮助!
我从答案中得到的是,如果 new
未被触发,我不需要 delete
任何东西。 delete[]
和 new[]
也是如此。通常创建的数组,如我的问题 int myArray[]
中的数组,不是动态创建的,因此不需要在析构函数中动态删除,这意味着,编译器会为我完成删除部分。谢谢大家
我是 C++ 的新手,所以我在内存管理方面苦苦挣扎。
我知道当我们有一个指向数组的指针时,这意味着它是动态创建的,所以我应该稍后在我的析构函数中删除它。但是如果我们有一个普通数组怎么办,我是否也需要在这个普通数组的析构函数中包含一个 delete 语句?或者程序会自动执行?
例如,我有一个像这样的普通数组
int myArray[];
在我的析构函数中,我应该包括这个:
delete[] myArray;
?
提前致谢,
But what if we have a normal array, do I need to include a delete statement in the destructor for this normal array too? or the program will do that automatically? for example, I have a normal array like this
int myArray[];
and in my destructor, should I include this:delete[] myArray;
?
没有。如果您没有 new[]
数组,则不需要 delete[]
它。其他一切都是自动完成的。这就是为什么使用 std::array
、std::vector
等非常棒:使用它们时无需写 new[]
,因此您不必担心您可能会忘记一个delete[]
.
"I know that when we have a pointer to an array, it means it is dynamically created"
这是不正确的。您基本上可以使用指向任何内容的指针,包括堆栈变量。动态分配通常会给你一个指针(这可能是混淆的根源)但并不是每个指针都来自动态分配。考虑一下:
void foo()
{
int a;
int* b = &a; // No dynamic allocation.
}
同样,这里既然没有new
,就不会有delete
。
I know that when we have a pointer to an array, it means it is dynamically created
没有。这种归纳是不正确的。指针也可以指向非动态创建的数组。
so I should delete it later in my destructor.
如果您的 class 实例负责该数组的销毁,那么析构函数确实是一个可能应该完成的地方。
do I need to include a delete statement in the destructor for this normal array too? for example, I have a normal array like this int myArray[];
没有。只有使用 new
创建的对象才会使用 delete
销毁。只有用 new[]
创建的对象才会用 delete[]
销毁。没有别的。
or the program will do that automatically?
是的。
具有自动存储持续时间的对象在声明它们的范围结束时自动销毁。这就是存储名称 class 的来源。具有静态存储持续时间的对象在 main
returns 后销毁。临时对象在完整表达式结束时被销毁(除非通过将对象绑定到引用来延长它们的生命周期)。当超级对象(在此上下文中不表示基 class)对象被销毁时,成员变量也被销毁。
只有动态创建的对象必须手动销毁。
虽然了解它是如何完成的很有用,但很少需要在 C++ 中进行手动内存管理。标准库提供的容器可以为您完成繁重的工作。人们通常会使用 std::vector
.
内存管理对任何人来说都不是一件容易的事,尤其是对于那些刚接触 C++ 的人。我建议查看术语 stack
和 heap
,希望它们能澄清一些事情。
I know that when we have a pointer to an array, it means it is dynamically created, so I should delete it later in my destructor.
这不完全正确,指针只是一个变量,用于保存某物的地址(变量所在的位置)。您可以像这样创建指向任何变量的指针:
int a = 5;
int *aPtr = &a;
这并不意味着 a
或 aPtr
是动态的。
然而,这样创建的指针将是动态的,需要删除:
int *aPtr = new int(5);
这里最大的区别是 new
关键字。 new
生成动态内存(放入堆中的内容)。每次使用new
关键字,都要用delete
关键字与之匹配。
But what if we have a normal array, do I need to include a delete statement in the destructor for this normal array too? or the program will do that automatically? for example, I have a normal array like this
int myArray[];
and in my destructor, should I include this:delete[] myArray;
?
不,您不需要调用 delete[] myArray;
因为这个数组将在堆栈上生成。我的意思是,当您 return 退出创建该数组的函数时,该数组将被清除出内存。
手动管理内存的另一种方法是使用 std::array
、std::vector
,甚至 std::unique_ptr
或 std::shared_ptr
等智能指针,它们会在对它的引用不见了。
希望对您有所帮助!
我从答案中得到的是,如果 new
未被触发,我不需要 delete
任何东西。 delete[]
和 new[]
也是如此。通常创建的数组,如我的问题 int myArray[]
中的数组,不是动态创建的,因此不需要在析构函数中动态删除,这意味着,编译器会为我完成删除部分。谢谢大家