[英]If we return address of local variable of function using pointer, do we need to delete the pointer after using that function or not?
If I don't use delete ptr
at comment here in below code, then it causes a memory leak?如果我在下面代码的注释中不使用
delete ptr
,那么它会导致 memory 泄漏吗?
int *create(){
int a;
int *a_ptr{&a};
return a_ptr;
}
int main(){
int *ptr = {nullptr};
ptr = create();
//here
return 0;
}
Actually you return pointer to object that is destroyed after create
function ends its execution.实际上,您返回指向 object 的指针,该指针在
create
function 结束执行后被销毁。 int a;
is created on stack so a_ptr points to some place on stack.在堆栈上创建,因此 a_ptr 指向堆栈上的某个位置。 During return all objects on stack are destroyed and there is only a_ptr left there as value.
在返回期间,堆栈上的所有对象都被销毁,并且只剩下 a_ptr 作为值。
No, you don't have memory leak but ptr
in main()
function is invalid as points to non-existing object.不,您没有 memory 泄漏,但
main()
function 中的ptr
无效,因为指向不存在的 object。
In main()
scope you initialized a pointer to nullptr
without allocating any space (You have not used the new
keyword).在
main()
scope 中,您初始化了指向nullptr
的指针,而没有分配任何空间(您没有使用new
关键字)。
In create()
function you declared an int without initializing it (it could have any value), then you declared a a_ptr
pointing to the reference of a
.在
create()
function 中,您声明了一个 int 而不初始化它(它可以有任何值),然后您声明了一个a_ptr
指向a
的引用。 When the code exits from the create()
function scope, the variable a
is out of scope and those memory cells will be marked as unused . When the code exits from the
create()
function scope, the variable a
is out of scope and those memory cells will be marked as unused . So, when the returned pointer it's assigned to the ptr
in main()
scope it will points to unused memory and this will lead to undefined behaviour whenever you use this pointer.因此,当返回的指针被分配给
main()
scope 中的ptr
时,它将指向未使用的 memory 并且每当您使用此指针时都会导致未定义的行为。
Essentialy, the pointer is already pointing to nullptr
and you don't have to delete
it, because you haven't allocated any space for the pointer and you have nothing to delete.本质上,指针已经指向
nullptr
并且您不必delete
它,因为您没有为指针分配任何空间并且您没有什么要删除的。
No, you must only delete
pointers that were created by new
.不,您只能
delete
由new
创建的指针。 Never delete pointers to local variables.永远不要删除指向局部变量的指针。 However, your code has undefined behaviour and that should be your primary concern.
但是,您的代码具有未定义的行为,这应该是您最关心的问题。
When create()
returns, the lifetime of it's local variable a
will end and any pointers/references to it will become invalid.当
create()
返回时,它的局部变量a
的生命周期将结束,任何指向它的指针/引用都将变得无效。 Accessing such an invalid pointer causes undefined behaviour which often manifests as segmentation fault.访问这样的无效指针会导致未定义的行为,这通常表现为分段错误。 Never return pointers or references to local variables from a function.
切勿从 function 返回指向局部变量的指针或引用。
One option is to actually new
the pointer you return:一种选择是实际
new
您返回的指针:
int *create(){
return new int{0};
}
int main(){
int *ptr = create();
delete ptr;
}
I wouldn't recommend this however.不过我不推荐这个。 In modern C++ you can and should avoid
new
/ delete
/ new[]
/ delete[]
and prefer smart pointers or vectors as appropriate.在现代 C++ 中,您可以而且应该避免使用
new
/ delete
/ new[]
/ delete[]
并酌情选择智能指针或向量。
If I don't use delete ptr at comment here in below code, then it causes a memory leak?
如果我在下面代码的注释中不使用 delete ptr,那么它会导致 memory 泄漏吗?
No. Variables with automatic storage duration are destroyed automatically when they go out of scope.没有。具有自动存储持续时间的变量在 go 超出 scope 时会自动销毁。 There is no memory leak in the example.
示例中没有 memory 泄漏。
However if you do delete the ptr then the behaviour of the program will be undefined.但是,如果您确实删除了 ptr,那么程序的行为将是未定义的。 That's because you may
delete
only what you've allocated with new
.那是因为你只能
delete
你用new
分配的东西。
Even if you only indirect through the pointer, will the program be undefined.即使您只是间接通过指针,程序也会未定义。 The returned pointer is always invalid and nothing useful can be done with it.
返回的指针总是无效的,不能用它做任何有用的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.