Solved

doubt with scope of smart pointers

Posted on 2004-08-19
5
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

696 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