C++ 的参数传递具有动态分配内存的对象
我是 C++ 世界的新手,但我有一些 C 经验并阅读了一些有关 C++ 的教程。 现在,在 C++ 中创建对象似乎非常容易,并且只要该类仅具有值(而不是指针)的属性,对我来说就可以很好地工作。
现在,当我尝试创建在构造函数中为其某些属性分配内存的对象时,我会弄清楚这些对象是如何在函数之间传递的。 此类的一个简单示例是:
class A {
int *a;
public:
A(int value) {
this->a = new int;
*(this->a) = value;
}
~A() {
delete this->a;
}
int getValue() const { return this->a; }
}
我想使用该类并将其按值传递给其他函数等。至少这些示例必须能够正常工作,而不会产生内存泄漏或双重释放错误。
A f1() {
// some function that returns A
A value(5);
// ...
return value;
}
void f2(A a) {
// takes A as a parameter
// ...
}
A a = f1();
A b = a;
f2(a);
f2(f1());
类 A
不完整,因为我应该重写 operator=
和 A(A& oldValue)
来解决其中一些问题。 据我了解,这些方法的默认实现只是复制成员的值,这导致析构函数在相同的指针值上被调用两次。
我对吗?我还缺少什么? 另外,您知道有什么好的教程可以解释这个问题吗?
I'm new to the C++ world, but I have some experience with C and read some tutorials about C++.
Now, creating objects in C++ seems quite easy and works well for me as long as the class has only attributes that are values (not pointers).
Now, when I try to create objects which allocate memory in the constructor for some of their attributes, I figure out how exactly such objects are passed between functions.
A simple example of such class would be:
class A {
int *a;
public:
A(int value) {
this->a = new int;
*(this->a) = value;
}
~A() {
delete this->a;
}
int getValue() const { return this->a; }
}
I want to use the class and pass it by value to other functions, etc. At least these examples must work without creating memory leaks or double free errors.
A f1() {
// some function that returns A
A value(5);
// ...
return value;
}
void f2(A a) {
// takes A as a parameter
// ...
}
A a = f1();
A b = a;
f2(a);
f2(f1());
The class A
is incomplete because I should override operator=
and A(A& oldValue)
to solve some of these problems.
As I understand it, the default implementation of these methods just copy the value of the members which is causing the destructor to be called twice on the same pointer values.
Am I right and what else am I missing?
In addition, do you know any good tutorial that explains this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用容器和智能指针。
例如,
std::vector
用于动态长度数组,或boost::shared_ptr
用于动态分配的单个对象。不要直接处理对象生命周期管理。
干杯&呵呵,
Use containers and smart pointers.
E.g.
std::vector
for dynamic length array, orboost::shared_ptr
for dynamically allocated single object.Don't deal directly with object lifetime management.
Cheers & hth.,
当您传递这样的对象时,您将创建该对象的副本。为了避免这样做,您应该传递一个 const 引用...
这确实意味着您不允许在函数中更改“a” - 但是,说实话,您无论如何都不应该这样做,因为任何更改都会获胜不会被反射回传入的原始参数。因此,编译器在这里帮助您,但在您犯了很难发现的错误时不会编译。
When you pass an object like that, you will create a copy of the object. To avoid doing that, you should pass a const reference...
This does mean that you are not allowed to change 'a' in your function - but, to be honest, you shouldn't be doing that anyways, as any changes won't be reflected back to the original parameter that was passed in. So, here the compiler is helping you out, but not compiling when you would have made a hard to find error.
具体来说,您必须实现一个复制构造函数,以正确复制a变量的内存指针。任何默认构造函数都会简单地复制 a 变量的内存位置,这显然会受到双重删除的影响。
即使这样做:也
不会起作用,因为当 A 超出范围(在该部分的末尾)时,A 的删除运算符将被调用,从而删除 a 子变量并占用内存无效的。
Specifically, you must implement a copy constructor that properly copies the memory pointer for the a variable. Any default constructor would simply copy the memory location for the a variable, which would obviously be subject to a double-delete.
Even doing this:
won't work because when A falls out of scope (at the end of the section) the delete operator for A will be called, thus deleting the a sub-variable and making the memory invalid.