CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2011
    Posts
    1

    stl string memory grow

    Hi Everybody,

    I have a problem with string stl from visual studio (2005, 2008 and 2010). In my program, memory grows up to 1Gb.
    I create a little program that reproduce that:

    void main(void)
    {
    std::string strCtn = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

    std::string str;
    while( str != "bk" )
    {
    if( str != " " )
    {
    std::string strTemp;
    while( str != "bk" )
    {
    std::cout << "Enter \"bk\" to exit loop (1/2) [size=" << strTemp.size() << "]" << std::endl;
    std::cin >> str;
    strTemp += strCtn;
    }
    }
    std::cout << "Enter \"bk\" to exit loop (2/2)" << std::endl;
    std::cin >> str;
    }
    std::cout << "Enter a key to end program" << std::endl;
    std::cin >> str;
    }

    When I enter severals time in the second loop, memory grows. But when I exit the loop, memory is not free. I enter again several times in the second loop, I must loop more than the previous time to see memory up.
    It seems stl string manage its own memory, but when memory up to 1Gb, it becomes a problem, my program crash.

    How can I force stl free memory ?

    thanks in advanced

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: stl string memory grow

    Quote Originally Posted by lateteatutu View Post
    Hi Everybody,

    I have a problem with string stl from visual studio (2005, 2008 and 2010). In my program, memory grows up to 1Gb.
    I create a little program that reproduce that:
    Go back and re-edit your post to use code tags. The code you posted is unformatted and almost unreadable wihtout code tags.

    You have three strings, strCtn, str, and strTemp. Which one are you referring to?

    Secondly, the string is destroyed once it goes out of scope. Once the string is out of scope, anything to do with memory is dependent on the heap manager and OS, and has nothing to do with std::string.

    Regards,

    Paul McKenzie

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: stl string memory grow

    Define what you mean by "see memory up". You cannot reliably use the Task Manager to check memory consumption, FYI. It gives you only an upper bound.

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