If I declare an object inside a for field, is the memory freed after that?

If I declare and put inside a for object and for example assign its address to a pointer, then when it is for the end, will that pointer point to invalid memory? Like at the end of the method with the objects you specified in this method

+3


source to share


7 replies


Yes, not just inside - inside any block.

For example:

 void bar()
 {
   foo* p;
   {
     foo f;
     p = &f;
   }
   // p no longer points to a valid object, f has ended its lifetime

      

Same with for

, except that the object will be created / destroyed every time the loop starts.



Now, if instead you have:

 void bar()
 {
   foo* p;
   {
     foo* f = new foo;
     p = f;
   }
   // p is still valid here, you need to clean up yourself with delete

      

And if you do this inside a for, you need to be very careful not to leak all of these distributions.

+7


source


Objects created on the stack (not using new / malloc / etc ...) are destroyed at the end of the scope. So yes.



Objects created on the heap (using new / malloc / etc ...) are missing and you need to call delete / free / etc .. on them

+2


source


Yes.

A for-statement has the following grammar:

for (init cond; expr) statement

      

And it's equivalent:

{
    init
    while (cond)
    {
        statement
        expr;
    }
}

      

Thus, anything that has been defined inside the for loop ends its lifetime at the end of the loop, both for the inner expression and for the complete control structure. Pointing out that he is no longer alive, UB.

Generally:

void* p;

{
    T x;
    p = &x;
} // x no longer exists

// p no longer holds a valid value

      

+2


source


The variable will be on the stack and out of scope when the for-loop ends. If you allocate memory on the heap and assign it to a pointer declared in the for-loop, then the memory will have a dangling reference when the for loop completes, because the local reference no longer exists.

+1


source


Any object declared inside a block {

and }

is destroyed after exiting that block, regardless of whether the block is used as part of a compound statement (for example, the body of a loop for

). All pointers to such objects are also invalidated.

EDIT as correctly stated by 0A0D, the lifetime of objects created with is new

explicitly controlled; they will survive block exits, but they also need to be removed manually.

+1


source


If you statically declare it, yes. If it is dynamic, then no.

For example this one will be released

for (...)
{
   Foo f;
}

      

but this one won't :

for (...)
{
    Foo * f = new Foo();
}

      

+1


source


Do you mean:

for(init; condition; upd)
 {
     ThisIsAClass anObject;
     // do something, like even assigning the obj address to
     // a var declared outside the loop
 } 

 // here, the pointer to anObject can be used?

      

The answer is no. It (the object) "disappears" out of scope.

0


source







All Articles