C+中的内存分配dilema+; 考虑一个C++函数为: A* dum() { A* a = new A() doSomethingOnA() return a; }
现在,C+中的内存分配dilema+; 考虑一个C++函数为: A* dum() { A* a = new A() doSomethingOnA() return a; },c++,memory-management,C++,Memory Management,现在,dum()被称为 A* b; b = dum(); 我的问题是我们是否也需要为b分配内存 编辑:如果我按照以下方式进行操作会怎么样: bool dum(A* a) { // A* a = new A() -- Is this needed? doSomethingOnA() return 1; } dum被称为 A* b = new A() dum(b); 您不需要将新的a()分配给b,因为通过调用b=dum(),您已经使b指向与a相同的内存位置。请记住,通过使用指针,您
dum()
被称为
A* b;
b = dum();
我的问题是我们是否也需要为b
分配内存
编辑:如果我按照以下方式进行操作会怎么样:
bool dum(A* a)
{
// A* a = new A() -- Is this needed?
doSomethingOnA()
return 1;
}
dum被称为
A* b = new A()
dum(b);
您不需要将新的a()分配给b,因为通过调用b=dum(),您已经使b指向与a相同的内存位置。请记住,通过使用指针,您不会将内容分配给b,而是只分配一个内存位置地址
事实上,如果您先执行b=new A(),然后执行b=dum(),那么首先会丢失b指向的内存位置上的引用,这将导致内存泄漏。从RAII的角度来看,您不应该在
dum
内部分配并返回原始指针
更好的选择是:
- std::unique_ptr dum()李>
- std::shared_ptr dum()李>
- 无效dum(A&)李>
- 达姆()李>
- 你的两个例子都是合法的。正如其他人已经建议的,有更好的方法,但原则上您的代码是好的。我试图添加一些评论来解释发生了什么。希望这能澄清这一点
和“否-不需要为b保留内存”。编译器为您处理它
你的第一个例子
在另一个功能中:
void func()
{
A* b; // The compiler reserves memory for holding
// the pointer b on the stack.
b = dum(); // The value of the pointer b is changed
// so it points to the instance of class A
// which is in the heap. The pointer b is
// still on the stack.
// ... more code
delete b; // Delete b to free the heap memory
// (and call the class A destructor).
return; // This will release
// the stack memory used for holding the pointer b.
}
你的第二个例子
原则是一样的
和“不-您不需要在函数dum(..)中使用新的。
事实上,这将是一个错误
bool dum(A* a)
{
// As this function has no local variable, no stack
// memory will be needed for holding variables.
// A* a = new A() -- Is this needed? NO - it would be a bug
doSomethingOnA(a);
return 1;
}
void func()
{
A* b; // The compiler reserves memory for holding
// the pointer b on the stack.
b = new A(); // On run time you reserve memory on the heap
// for holding an instance of class A.
// The value of the pointer b is changed
// so it points to the instance of class A
// which is in the heap. The pointer b is
// still on the stack.
dum(b); // Just a function call
// ... more code
delete b; // Delete b to free the heap memory
// (and call the class A destructor).
return; // This will release
// the stack memory used for holding the pointer b.
}
注:
编译器可以优化堆栈的使用,使函数中的局部变量完全保存在寄存器中,因此根本不需要任何内存。什么叫“分配内存”?不,
b
是a
来自dum()
,因为a
指向数据,b
将指向与a
@Borgleader相同的地址-我的意思是performing b=new a()您不想为b分配内存,因为dum()会这样做。在调用dum()之前为b分配内存除非你释放它,否则会导致内存泄漏。@ MayankJain,为什么你使用原始指针和<代码>新()?/C>?在C++中通常不需要这样做。你可能会被java或C语言概念混淆。@π-γ-波恩αῥεῖ 应该只是(一个函数返回一个值-不是本地的)啊,对不起,我误解了你的示例,我认为它应该简单地演示A
的堆栈分配(这是一个值得一提的问题)。关于源代码表示,你应该保留我的其他编辑。@πάνταῥεῖ 品味的问题(这样更简洁,它没有引用任何代码)如果你展示代码,你应该这样呈现它(因为它提高了可读性。至少对我来说是这样),但是品味的问题可能会。
bool dum(A* a)
{
// As this function has no local variable, no stack
// memory will be needed for holding variables.
// A* a = new A() -- Is this needed? NO - it would be a bug
doSomethingOnA(a);
return 1;
}
void func()
{
A* b; // The compiler reserves memory for holding
// the pointer b on the stack.
b = new A(); // On run time you reserve memory on the heap
// for holding an instance of class A.
// The value of the pointer b is changed
// so it points to the instance of class A
// which is in the heap. The pointer b is
// still on the stack.
dum(b); // Just a function call
// ... more code
delete b; // Delete b to free the heap memory
// (and call the class A destructor).
return; // This will release
// the stack memory used for holding the pointer b.
}