Solved

a question on memory scope

Posted on 2004-08-25
6
227 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

943 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

4 Experts available now in Live!

Get 1:1 Help Now