CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2009
    Posts
    17

    Is it critical to release variable memory in a function?

    Hello,
    suppose we declared and initialized two variable in a function like this:
    Code:
    int function test()
    {
    CString str; int index; str="this is a test"; i=0; //other statements to manipulate these variables return TRUE;
    }
    Now suppose we call this function from main function.Am I must release the memory allocated by str and index variables or they will be released automatically after termination of the function?
    if the application ended, memories that allocated by these 2 variables will release automatically or I have to release them manually?
    Please consider that one of them (str) is a Class and another (index) is a base variable.
    Thanks alot !
    Last edited by soroush_vs; April 13th, 2009 at 03:54 AM.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Is it critical to release variable memory in a function?

    The way you use them here, they will be freed automatically.
    However, if you would define them as follows:
    Code:
    CString* pStr = new CString();
    Then you need to free them:
    Code:
    delete pStr;
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Tags for this Thread

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