Solved

a question on memory scope

Posted on 2004-08-25
6
241 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 36
C++ get user from AD  (VS6) 7 86
How do I save this device context as a .bmp or a picture of some kind? 2 64
learn programming 8 42
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 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