简体   繁体   English

C++ 释放函数中堆分配的变量

[英]C++ freeing heap-allocated variables in functions

Here's part of a function:这是函数的一部分:

std::queue<std::string>* shunting_yard(std::vector<std::string> input){
    std::queue<std::string>* output = new std::queue<std::string>();
    ...
    return output;

I've dynamically allocated queue output is pointing to on the heap.我动态分配的队列output指向堆上。 However, when I try to free/delete the queue in int main() I get an error because output is declared out of scope.但是,当我尝试在int main()释放/删除队列时,我收到一个错误,因为output被声明为超出范围。

Considering the fact that I'm returning output in the above function- so I can't free it inside the function- how can I make sure that there is not a memory leak?考虑到我在上述函数中返回output的事实 - 所以我不能在函数内部释放它 - 我如何确保没有内存泄漏? One potential solution I've thought of would be declaring std::queue<std::string>* output in the global scope and output = new std::queue<std::string>() inside the function, but are there any other solutions to this out of scope error?我想到的一种潜在解决方案是在全局范围内声明std::queue<std::string>* output ,并在函数内声明output = new std::queue<std::string>() ,但是否存在这个超出范围的错误还有其他解决方案吗?

You do not delete variables, or pointers.您不会删除变量或指针。 You delete things you allocate with new .你删除你用new分配的东西。 The line:线路:

std::queue<std::string>* output = new std::queue<std::string>();

does not allocate a variable called output with new .不会使用new分配名为output的变量。 It allocates a queue, which has no name, and creates a local variable called output , and stores the address of the queue into the local variable output .它分配一个没有名称的队列,并创建一个名为output的局部变量,并将队列的地址存储到局部变量output

This line:这一行:

return output;

returns the address of the queue.返回队列的地址。 Because it also ends the function, the local variable output is destroyed (just like all local variables).因为它也结束了函数,所以局部变量output被销毁(就像所有局部变量一样)。 The caller receives the address.调用者接收地址。

How do you call this function?你如何调用这个函数? You didn't show that code, but it's something like:您没有显示该代码,但它类似于:

std::queue<std::string>* woof = shunting_yard(meow);

In this case, this creates a new local variable in the caller, called woof , which now holds the address of the queue that shunting_yard created.在这种情况下,这会在调用者中创建一个新的局部变量,称为woof ,它现在保存了shunting_yard创建的队列的地址。

You can delete the queue by passing its address to delete .您可以通过将其地址传递给deletedelete队列。 Its address is stored in the variable woof .它的地址存储在变量woof So you can write delete woof;所以你可以写delete woof; . . The fact that the address was stored in a different variable is irrelevant, as long as the address is the same.只要地址相同,地址存储在不同变量中的事实就无关紧要。


More advanced programmers will use std::unique_ptr<std::queue<std::string>> .更高级的程序员将使用std::unique_ptr<std::queue<std::string>> But I recommend learning to do new and delete by hand first , before learning unique_ptr , so that you'll understand what unique_ptr actually does.但我建议学习做newdelete学习之前用手, unique_ptr ,这样你就会明白unique_ptr实际上做。

Really advanced programmers will realize they don't new at all, and just use std::queue<std::string> (no pointer).真正高级的程序员会意识到他们根本不new ,只使用std::queue<std::string> (无指针)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM