Reputation: 171
Suppose we have the following C++ code:
while(condition)
{
Node* temp = SomeNode->next;
//...
}
destructor()
{
//delete Nodes;
}
And here temp
is a local pointer variable and CurrentNode
is a public class property. The public nodes and relevent nodes get freed in the destructor. But what happens to all the local scoped pointer variables. A pointer data type consumes some memory, for example 4 bytes or more (depending on the memory space), just for the address. Here, The temp
local variable is not initialized with 'new' and is just a pointer to some other nodes, based on this, Is temp
local variable made on the stack or on the heap? Does this local variable get destructed automatically soon after exiting the block or is it dynamic memory and yet needs using of delete
. I saw some codes somewhere that did not use delete
to free up local pointers that were not made with new
. And I want to make sure if he missed freeing up the variables or there is some other story about it that I do not know. Thanks.
Upvotes: 1
Views: 1125
Reputation: 1389
The concept of memory management is to delete memory that you have allocated with new, alloc or malloc otherwise there will be some memory leak. Apparently the only way to manage memory allocated is to delete. This, however can be done in the destructor. Which, means only class variable gets deleted. So there is no way to delete local variables that are defined within a function. As they only exist within their scope.
When handling local variables, some technics are required i.e you can use class variables inside a function that will eventually get deleted or you can declare a pointer that will only be pointing to some memory address without actually allocating it some memory.
Upvotes: 1
Reputation: 18964
The pointer temp
is on the stack; the Node
it points to is on the heap.
All local variables are destroyed automatically when you exit the containing block. For pointers and other simple things (ints, bools) this destruction doesn't need to do anything apart from reclaim the stack space. For variables of class type the destructors will run, allowing them to do any extra cleanup needed.
Also notice that if you did delete temp;
you'd be asking for the memory pointed to by temp to be freed; temp itself would still exist and you could assign a new value to it later.
Upvotes: 1
Reputation: 5970
The variable temp
is a pointer to a Node
object. Depending on your system it will most likely be a single integer storing a memory offset.
When you make a pointer like this it is allocated on the stack and reclaimed at the end of the scope:
{ // Enter Scope
Object* pObj; // A local variable created on the stack
} // pObj is reclaimed as would any other local variable.
The problem with memory leaks comes when you dynamically allocate memory (using new
or new[]
) and assign a pointer to the memory address and don't delete
(or delete[]
) the memory before the pointer goes out of scope and is reclaimed (at which point you have no way of deallocating the allocated memory).
{ // Enter Scope
Object* pObj = new Object; // Local pointer to an Object in the heap
} // pObj is reclaimed but the Object which it was pointed to is not.
// We have a memory leak.
As to whether you need to delete
(or delete[]
) the temp
variable, that all depends on the implementation of the Node::next
function and the "contract" that you have when calling it.
Upvotes: 2
Reputation: 1145
If the variable isn't allocated with new
(or some other function that is documented to return something that is allocated on the heap, like malloc
, make_shared
, or whatever), then it has automatic storage.
When the containing scope exits, the variable is destroyed. This is an extremely important concept if you're working in C++, as it forms the fundamental basis for how memory management and other resource management should be done. Search for RAII for more details.
Upvotes: 2
Reputation: 1598
The temp variable is a pointer that is pushed onto the stack. It gets released after it falls out of scope which is your "While" statement.
Upvotes: 4