Solved

a question on memory scope

Posted on 2004-08-25
6
219 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:lantianman
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now