在没有 'new' 的情况下动态创建数组时的 C++ 垃圾回收
C++ garbage collection when array is dynamically created without 'new'
我只是偶然发现我们可以在没有 'new' 的情况下动态创建数组
(Dynamic array without new (C++))。
例如,这是可能的:
cout<<"enter length: ";
cin>>length;
int a[length];
我的问题是:既然我们没有使用'new'来动态分配内存,我们是否应该(或可以)使用'delete'来销毁对象?
谢谢,
在 C++ 中,没有垃圾回收。我们需要删除在堆上分配的所有项目。在你的代码中,当你创建一个没有 new 的数组时,你不会在堆上分配任何 space ,你只使用有限的堆栈。如果不需要,您应该避免使用 new
关键字,但是如果您需要使用它,请不要忘记为您在堆上分配 space 的每个项目使用 delete
.对于你的问题,如果你没有在堆上分配 space 就不需要删除对象。
int a[length]; // allocates an array on the stack
int *a = new int[length]; // allocates an array on the heap
您创建了一个 int a [size]
数组,该数组被压入函数堆栈,并在函数结束时被移除。不需要调用删除运算符。您的数组不是动态的。
来自cplusplus.com tutorial on operator new
:
Dynamic memory is allocated using operator new. new is followed by a
data type specifier and, if a sequence of more than one element is
required, the number of these within brackets []. It returns a pointer
to the beginning of the new block of memory allocated.
In most cases, memory allocated dynamically is only needed during
specific periods of time within a program; once it is no longer
needed, it can be freed so that the memory becomes available again for
other requests of dynamic memory.
你应该问吗?
我认为您不应该使用此功能。分配可变长度、连续元素列表的 C++ 方法是使用 std::vector<int>
。这将为您提供漂亮、可移植的代码,并且您不必关心依赖于编译器的行为、堆栈大小或任何其他限制。
但是,如果你还想问,我们得先决定
问谁?
其他答案暗示这是一个关于标准语言c++的问题。它不是。正如评论所指出的,您编写的代码不符合标准的 c++。当标准中的概念不适用时,用标准中的概念来推理它毫无意义*.
此代码只能使用为您提供允许您拥有可变长度数组的扩展的编译器进行编译。因此,您必须询问您的编译器制造商。
gcc
如果您使用的是 gcc/g++,their specification 是相关文档,在他们的例子中,它说:
The storage is allocated at the point of declaration and deallocated when the block scope containing the declaration exits.
所以:不,您不必手动删除或释放此内存。
铿锵
如果您使用的是 clang,this page 说明:
However, Clang supports such variable length arrays for compatibility with GNU C and C99 programs.
因此,您可以期望它们的扩展与 C99 兼容,这意味着您也不必手动释放任何东西。
msvc
This question表示msvc不支持VLA,所以在这里,你也不需要手动释放任何东西。
* 当然,当添加一个语义看起来像你在使用局部变量的扩展时,这个变量的行为也像一个局部变量是有道理的,所以从开发 gcc 的人那里,我希望这与 c++ 中的其他行为一致,但理论上,编译器制造商可以根据需要构建他们的编译器,并且他们也可以使此扩展期望您手动释放此内存。
我只是偶然发现我们可以在没有 'new' 的情况下动态创建数组 (Dynamic array without new (C++))。 例如,这是可能的:
cout<<"enter length: ";
cin>>length;
int a[length];
我的问题是:既然我们没有使用'new'来动态分配内存,我们是否应该(或可以)使用'delete'来销毁对象? 谢谢,
在 C++ 中,没有垃圾回收。我们需要删除在堆上分配的所有项目。在你的代码中,当你创建一个没有 new 的数组时,你不会在堆上分配任何 space ,你只使用有限的堆栈。如果不需要,您应该避免使用 new
关键字,但是如果您需要使用它,请不要忘记为您在堆上分配 space 的每个项目使用 delete
.对于你的问题,如果你没有在堆上分配 space 就不需要删除对象。
int a[length]; // allocates an array on the stack
int *a = new int[length]; // allocates an array on the heap
您创建了一个 int a [size]
数组,该数组被压入函数堆栈,并在函数结束时被移除。不需要调用删除运算符。您的数组不是动态的。
来自cplusplus.com tutorial on operator new
:
Dynamic memory is allocated using operator new. new is followed by a data type specifier and, if a sequence of more than one element is required, the number of these within brackets []. It returns a pointer to the beginning of the new block of memory allocated.
In most cases, memory allocated dynamically is only needed during specific periods of time within a program; once it is no longer needed, it can be freed so that the memory becomes available again for other requests of dynamic memory.
你应该问吗?
我认为您不应该使用此功能。分配可变长度、连续元素列表的 C++ 方法是使用 std::vector<int>
。这将为您提供漂亮、可移植的代码,并且您不必关心依赖于编译器的行为、堆栈大小或任何其他限制。
但是,如果你还想问,我们得先决定
问谁?
其他答案暗示这是一个关于标准语言c++的问题。它不是。正如评论所指出的,您编写的代码不符合标准的 c++。当标准中的概念不适用时,用标准中的概念来推理它毫无意义*.
此代码只能使用为您提供允许您拥有可变长度数组的扩展的编译器进行编译。因此,您必须询问您的编译器制造商。
gcc
如果您使用的是 gcc/g++,their specification 是相关文档,在他们的例子中,它说:
The storage is allocated at the point of declaration and deallocated when the block scope containing the declaration exits.
所以:不,您不必手动删除或释放此内存。
铿锵
如果您使用的是 clang,this page 说明:
However, Clang supports such variable length arrays for compatibility with GNU C and C99 programs.
因此,您可以期望它们的扩展与 C99 兼容,这意味着您也不必手动释放任何东西。
msvc
This question表示msvc不支持VLA,所以在这里,你也不需要手动释放任何东西。
* 当然,当添加一个语义看起来像你在使用局部变量的扩展时,这个变量的行为也像一个局部变量是有道理的,所以从开发 gcc 的人那里,我希望这与 c++ 中的其他行为一致,但理论上,编译器制造商可以根据需要构建他们的编译器,并且他们也可以使此扩展期望您手动释放此内存。