[英]Do I have to delete memory in this case?
Here is a simple program using dynamic memory. 这是一个使用动态内存的简单程序。
My question is do I have to delete the memory at the and or the struct will take care of it for me? 我的问题是我是否必须删除和上的内存,否则该结构将为我处理它?
#include <iostream>
struct Student {
int grade;
char *name;
};
void print(Student name);
int main() {
Student one;
one.grade = 34;
one.name = new char[12];
int i;
for (i = 0; i < 11; ++i) {
one.name[i] = 'a' + i;
}
one.name[i] = '\0';
print(one);
delete[] one.name;
return 0;
}
void print(Student name) {
std::cout << name.name << " has a score of " << name.grade << "\n";
}
There is a simple rule of thumb- for each call of new
you should have one call of delete. 有一条简单的经验法则:每次调用
new
您都应该删除一次。 In this case you should delete one.name like so : delete [] one.name
. 在这种情况下,您应该像这样删除one.name:delete
delete [] one.name
。
Of course you should do that after you no longer need its value. 当然,在不再需要它的价值之后,您应该这样做。 In this case this is immediately before the return.
在这种情况下,这正好在返回之前。
Memory allocated dynamically
using new
or malloc
must be freed up when you're done with it using delete
or free
otherwise you'll get Memory leak
. 使用
delete
或free
完成使用new
或malloc
dynamically
分配的malloc
必须释放它,否则会出现Memory leak
。
Make difference between delete
and delete[]
: the first without subscript operator is used to deleted dynamic memory allocated with new for a pointer. 区分
delete
和delete[]
:第一个不带下标的运算符用于删除为new分配的指针动态内存。 The latter is used for deleting an array allocated dynamically. 后者用于删除动态分配的数组。
So in your case: 因此,在您的情况下:
one.name = new char[12]; // an array of 12 elements in the heap delete[] one.name; // freeing up memory char* c = new char('C'); // a single char in the heap delete c;
Don't mix new, delete
with malloc, free
: 不要混用
new, delete
使用malloc, free
new, delete
malloc, free
:
This is undefined behavior, as there's no way to reliably prove that memory behind the pointer was allocated correctly (ie by new for delete or new[] for delete[]). 这是未定义的行为,因为无法可靠地证明指针后面的内存已正确分配(即,对于delete来说是new,对于delete []是new [])。 It's your job to ensure things like that don't happen.
确保这样的事情不会发生是您的工作。 It's simple when you use right tools, namely smart pointers.
使用正确的工具(即智能指针)很简单。 Whenever you say delete, you're doing it wrong.
每当您说删除时,您就做错了。
如果您不想使用唯一/共享的指针,则可以使用构造函数进行分配,并使用析构函数自动释放内存。
If the raw pointer in your structure is an observing pointer, you don't have to delete the memory (of course, someone somewhere in the code must release the memory). 如果结构中的原始指针是观察指针,则不必删除内存(当然,代码中某处的人必须释放内存)。
But, if the raw pointer is an owning pointer, you must release it. 但是,如果原始指针是所有者指针,则必须释放它。
In general, every call to new[]
must have a matching call to delete[]
, else you leak memory. 通常,对
new[]
每次调用都必须具有对delete[]
的匹配调用,否则会浪费内存。
In your code, you invoked new[]
, so you must invoke delete[]
to properly release the memory. 在您的代码中,您调用了
new[]
,因此必须调用delete[]
才能正确释放内存。
In C++, you can avoid bug-prone leak-prone raw pointers, and use smart pointers or container classes (eg std::vector
, std::string
, etc.) instead. 在C ++中,您可以避免容易出错的易于泄漏的原始指针,而改用智能指针或容器类(例如
std::vector
, std::string
等)。
Creating a Structure does not mean it will handle garbage collection in C++ there is no garbage collection so for every memory you allocate by using new you should use delete keyword to free up space. 创建结构并不意味着它将在C ++中处理垃圾回收,因此没有垃圾回收,因此对于使用new分配的每个内存,都应使用delete关键字释放空间。 If you write your code in JAVA you wont have to delete as garbage collector will automatically delete unused references.
如果您用JAVA编写代码,则不必删除,因为垃圾收集器会自动删除未使用的引用。
Actually the structure won't freeup or delete the memory that you have been created. 实际上,该结构不会释放或删除您已创建的内存。 if in case you want to freeup the space you can do as follows.
如果要释放空间,可以执行以下操作。
#include <iostream>
using namespace std;
int main()
{
char *c = new char[12];
delete[] c;
return(0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.