Link to home
Start Free TrialLog in
Avatar of lantianman
lantianman

asked on

doubt with scope of smart pointers

I am not sure when a piece of memory a referred by a smart pointer a_sp is freed. Is it freed after a_sp being used the last time or at the end of the function where a_sp is declared? Thank you!
Avatar of ikework
ikework
Flag of Germany image

what kind of smart-pointers are you using, an own class?
>>at the end of the function where a_sp is declared?

It's free when the variable goes out of scope.
So in most cases, it would be free when the function ends.

Continue...
Example:

void function()
{
   MySmartPtrType foofoo;
  //..some code here

}//foofoo gets free here

However, if your smart pointer is inside a sub section of your function, then it would get freed when the sub section ends.

Example

void function()
{
  for( int i = 0;i < 123;++i)
  {
        MySmartPtrType foofoo;
   }
    //foofoo gets free here because it goes out of scope before reaching the end of the function.

  //..some code here
}
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lantianman
lantianman

ASKER

Thank you!