Solved

a question on memory scope

Posted on 2004-08-25
6
250 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

738 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