Thread: Memory leak by simple char

  1. #1
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107

    Memory leak by simple char

    If you just use a simple char in your program, like this:

    Code:
    //Example code
    
    CHAR ChaSimple [ 32 ];
    
    strcpy ( ChaSimple, ChaOtherOne );
    
    strcat ( ChaSimple, "Some more text" );
    
    SetWindowText ( WinWindow, ChaSimple );
    After that you don't use ChaSimple anymore, will that cause memory leak if you just leave ChaSimple there?
    And if that piece of code gets called again, will it then also cause memory leakage?

    Thanks in advance, Ktulu.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >will that cause memory leak if you just leave ChaSimple there?
    No, only dynamic memory can be "leaked" in the way that most people describe it.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, there is no memory leak there. You did not dynamically allocate ChaSimple, so it will be cleaned up when the current scope ends. The functions strcpy, strcat and SetWindowText also don't dynamically allocate memory that you need to clean up.

    You just have to be careful that ChaOtherOne is 17 characters or less in length, or you will overwrite the bounds of ChaSimple.

  4. #4
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    Thanks for the reply's, but Daved, why can there only fit in 17 chars when I give it 32 array's?

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Because "Some more text" is 15 char-long if you count the null terminator and 17 + 15 = 32. If ChaOtherOne had more than 17 chars then it goes over 32 and you shoot yourself in the foot. Usually we use std::string over characters in C++ when messing with strings.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Because "Some more text" has a length of 14. So 14 + 17 = 31. Then add the string terminator and that makes 32. So ChaOtherOne better be 17 char or less. If it could possibly be longer, then make your array bigger.

  7. #7
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    Oh ok, it was only example anway/

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Oh ok, it was only example anway
    Just remember to apply the same logic to your actual code as well.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    A better way would be this:

    std::string ChaSimple = ChaOtherOne;
    ChaSimple += "Some more text";
    SetWindowText ( WinWindow, ChaSimple.c_str() );
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    And why is that, Cat?

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Using strings instead of char arrays can be a more effiecent way to achieve the same result

  12. #12
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    To be sure, when I declare a char string like this:

    CHAR ChaTest [ 4 ];

    And I will put in a string of 4 chars, like this:

    strcpy ( ChaTest, "Test" );

    Then ChaTest [ 0 ] will get filled with 'T', ChaTest [ 1 ] with 'e', ChaTest [ 2 ] with 's' and ChaTest [ 3 ] with 't', so that ChaTest [ 4 ] gets filled with '\0' by the strcpy function, right?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >right?
    Right, but since ChaTest [ 4 ] isn't a valid index, you've invoked undefined behavior by running past the end of your array. For the code to work, ChaTest would need to be defined with a size of 5.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This is another reason why the C++ string class is preferred. You don't have to worry about overstepping the bounds of the array. It will expand automatically as necessary. Also, it is much more intuitive to most programmers to use operators like = and += instead of functions like strcpy and strcat.

  15. #15
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Especially because this is the C++ board, why not use the better tools that C++ gives us for that.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM