Solved

a question on memory scope

Posted on 2004-08-25
6
236 Views
Last Modified: 2010-04-01
In the following code, will the memory refered by tmpiosVerStr or iosVerStr be freed after the if statement?

bool APLUSMSG::validateDiscrData(bool isContingIosFlag, IntPar &IntendVer, ostrstream& err)
{
...........
    StrPar iosVerStr;
...........
    if (isContingIosFlag)
    {
        iosVer = IntendVer.isSet() ? IntendVer.getVal() : parCBSC()->ContingIosVer();
        tmpDiscrValue = DiscrValue();
        tmpDiscrValueType = DiscrValueType();
        tmpDiscrFormCode = DiscrFormCode();
        tmpiosVerStr = IntendVer.isSet() ? "IntendedContingIosVer" : "ContingIosVer";
        iosVerStr.setVal(tmpiosVerStr);    // tmpiosVerStr passes a pointer to iosVerStr.
        DiscrValStr.setVal("DiscrValue");
        DiscrValTypeStr.setVal("DiscrValueType");
        DiscrFormCodeStr.setVal("DiscrFormCode");
    }
...........
}

Thanks for your help!
0
Comment
Question by:lantianman
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 11899801
>> will the memory refered by tmpiosVerStr or iosVerStr be freed after the if statement

iosVerStr - No, because it is defined outside the if block.  It will live until the end of the method.

tmpiosVerStr - Don't know but probably not.  It's definition does not appear in the code shown, but that means it's likely also outside the if block.
0
 

Author Comment

by:lantianman
ID: 11900599
But the memory refered by tmpiosVerStr is initialized inside the if block, will it be freed outside the block?
0
 
LVL 19

Expert Comment

by:drichards
ID: 11900666
tmpiosVerStr will have it's destructor called (and presumably free any memory it has alocated) when it goes out of scope.  Best guess is that it will happen at the end of the method.  Since the declaration of tmpiosVerStr does not appear in the code shown, it's only a guess.  It could be a global variable that lives for the entire life of the executable based on the code that's shown.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:lantianman
ID: 11900743
Thanks drichards. My problem is about scope of the const char "IntendedContingIosVer". when I write: iosVerStr.setVal("IntendedContingIosVer") inside a if block, someone told me it's equal to this:

const char* tmp = "IntendedContingIosVer";
iosVerStr.setVal(tmp);

When the if block ends, won't the tmp be released? Will the memory pointed by iosVerStr be invalid also?
0
 
LVL 2

Accepted Solution

by:
carribus earned 100 total points
ID: 11901579
In your code:

tmpiosVerStr = IntendVer.isSet() ? "IntendedContingIosVer" : "ContingIosVer";
iosVerStr.setVal(tmpiosVerStr);    // tmpiosVerStr passes a pointer to iosVerStr.

I assume tmpiosVerStr is defined as:

const char* tmpiosVerStr;

If you are asking about when the actual LITERAL string "IntendedContingIosVer" is cleaned up, well, then you need to understand that the compiler implicitly creates a constant memory block for the string (a literal), and the block will be cleaned up when the app exits (its a constant value).
However, when the call to :

iosVerStr.setVal(tmpiosVerStr);    // tmpiosVerStr passes a pointer to iosVerStr

I assume that the StrPar class will create a copy of the string that is passed in with the tmpiosVerStr pointer and is therefore responsible for cleaning up ITS local copy of the string when it destructs. As someone mentioned, your iosVerStr variable will destruct at the end of the function, not the end of the if block.

Am I shedding any light for you here?
0
 

Author Comment

by:lantianman
ID: 11903258
Thanks!
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 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