Solved

doubt with scope of smart pointers

Posted on 2004-08-19
5
222 Views
Last Modified: 2010-04-01
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!
0
Comment
Question by:lantianman
  • 3
5 Comments
 
LVL 20

Expert Comment

by:ikework
ID: 11842650
what kind of smart-pointers are you using, an own class?
0
 
LVL 30

Expert Comment

by:Axter
ID: 11842654
>>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...
0
 
LVL 30

Expert Comment

by:Axter
ID: 11842690
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
}
0
 
LVL 30

Accepted Solution

by:
Axter earned 150 total points
ID: 11842710
Correction:

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
}

In above example, foofoo goes out of scope, and therefor gets free over and over again, before reaching the end of the function.
So foofoo would get created and freed 123 times before the loop ends.
0
 

Author Comment

by:lantianman
ID: 11849443
Thank you!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question