Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

doubt with scope of smart pointers

Posted on 2004-08-19
5
Medium Priority
?
249 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 450 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

972 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