CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    Sep 2003
    Posts
    815
    thanks everyone

    but one (and most importent thing) I don't understand
    should I deallocate, and if so where?

    I use by reference (see the function declaration)

    #inclue <string>

    void BuildString(string &str
    {
    //string manipulation here

    }

    int main()
    {
    string myStr;
    BuildString(MyString);
    }

    thanks

  2. #17
    Join Date
    Dec 2002
    Posts
    47
    An STL string manages itself so you don't need to do anything. When the main() function returns, it calls the destructors of any variables allocated on the stack. The string will be deleted for you. This happens any time a variable loses "scope" ... see below:

    [code]#include <string>

    int main(int argc, char *argv[])
    {
    int x = 0;
    char text[100];

    // introduce new scope
    {
    // new scope
    std::string myString;
    myString = "some text";
    }
    // poof! myString is gone.
    }

    -rick

  3. #18
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Originally posted by fransn
    Vicondin:"The code was:

    code:--------------------------------------------------------------------------------
    string myStr;
    BuildString(MyString);

    "
    Yes but that's not the code I'm talking about. See the & there in the function declaration?

    void BuildString(string &str)
    {

    }
    Fransn: The original question was
    where does myStr is deallocated
    You are talking about the wrong code, then, since the code you are referencing with the following statement:
    See the & there in the function declaration
    has nothing to do with the life of myStr.
    "str", which is of type "reference to string" goes out of scope when BuildString returns - it's scope is the duration of BuildString. That is not to say that the destructor for the object that str is a reference to is called at this point.
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  4. #19
    Join Date
    Jul 2001
    Location
    Netherlands
    Posts
    751
    Dear Vicodin.
    please run this program and send me the money if you still have doubts.
    #include <string>
    typedef std::string string;

    void ChangeString(string & strData2)
    {
    strData2 = "This ain't worth my time";
    }
    int main(int argc, char* argv[])
    {
    string strData = " hoyhoy";
    ChangeString(strData);
    printf("%s\r\n",strData.c_str());
    if(stricmp(strData.c_str(),"This ain't worth my time") == 0)
    printf("vicodin owes fransn $50\r\n");
    else
    printf("fransn owes vicodin $50\r\n");
    return 0;
    }

  5. #20
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Fransn:
    I don't know what you don't get about the OP's question, and I fail to see the purpose of your little program there.
    The OP posted (in a nutshell):
    well I can use std:string so the code will be:
    Code:
    #inclue <string>
    void BuildString(string &str)
    {
    }
    
    int main()
    {
        string myStr; 
        BuildString(MyString);
    }
    my question where does myStr is deallocated?
    1. myStr is constructed within main, because that is where it is declared.
    2. One can only refer to the variable myStr, which is of type std::string, from within main.
    3. Even though a reference to myStr is passed into BuildString, the string itself is not referred to as myStr in that function. It is called "str".
    4. One can only refer to the variable str, which is of type "reference to std::string" from within BuildString.
    5. myStr goes out of scope when the function main exits. The memory associated with the variable is released.
    This (#5) answers the OP's question - "where does myStr is deallocated?".
    Your answer:
    no. It is passed in by reference and will get changed by reference and never leave scope
    besides being ambiguous, is partially incorrect. Your statement "it is passed in by reference and will get changed by reference" is correct, but has nothing to do with the OP's question. Your statement "and never leave scope" is incorrect because everything, in fact, has a scope.

    I can't believe I'm arguing such basics...
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  6. #21
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    ???

    ????!!!???

    Have I said that sizeof(Teste) is 6?

    ???

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured