简体   繁体   English

如果我们使用指针返回 function 的局部变量的地址,使用 function 后是否需要删除指针?

[英]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 .不,您只能deletenew创建的指针。 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.

相关问题 当我们通过引用传递时,为什么在调用函数时我们放置一个变量而不是地址(指针)? - When we pass by reference, why do we put a variable instead of address(pointer), when calling the function? 如何在不使用返回的情况下从函数中获取变量的地址(指针) - How to get the address (pointer) of a variable from a function without using return 为什么当一个函数接受一个指针时,我们传递的是一个特定变量的地址,而不是一个指针? - why when a function takes a pointer, then we pass the address of a specific variable, and not a pointer? 为什么我们将“ this”指针传递给setupUi函数? - Why do we pass “this” pointer to setupUi function? 如果我们知道参数和返回类型,如何使用其指针调用任何类函数? - How to call any class function using its pointer if we know arguments and return type? 使用指针打印功能地址 - Printing address of function by using pointer strtok_s()为什么我们需要指针的地址? - strtok_s() Why do we need address of a pointer? 通过函数返回指针地址 - Return address of pointer through function 我们如何从成员函数返回unique_pointer成员? - How do we return a unique_pointer member from a member function? 为什么我们要返回指向节点的指针而不是 void function 来实现 avl 树? - Why do we return a pointer to a node instead of void function for avl tree implementation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM