Thread: Memory leak by simple char

  1. #31
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Quote Originally Posted by Ktulu
    I don't want to use vectors because I don't know, I really don't wanna use code which I don't know yet.
    You should consider taking the time to learn them. The advantages they offer over regular arrays is similar to the advantages the C++ string class has over C style strings.

    However, you can still follow the advice with char arrays instead of vectors. The important point is that you should still be using C++ strings, and when you can't (like with GetWindowText) you try to minimize the use of C style strings to as small a scope as possible. Basically, follow the idea of Ken Fitlike's example below, except use char arrays instead of vector, and you will still be able to use C++ strings successfully.

  2. #32
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    Ok Cat.

    Quote Originally Posted by Mario F.
    any specific need for char arrays is quiet easily fulfilled with the c_str() member function. If you have a function needing a char array:

    void somefunction(char* buffer);
    or
    void somefunction(char[] buffer);

    The following will do it.
    Code:
    std::string foo = "pass me as c-style string";
    
    /*... do here all the nifty things C++ Strings allow you to do ...*/
    
    somefunction(foo.c_str());
    There are windows functions like 'GetWindowText' (and more!) were you can't parse a std :: string, were you need to use CHAR Buffers, so, how use them safely?

  3. #33
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You can use std::vector as an alternative, something like:
    Code:
    #include <vector>
    
    std::string GetWndTxt(const HWND hwnd)
    {
    int length=GetWindowTextLength(hwnd)+1;
    std::vector<char> tmp(length, '\0');
    GetWindowText(hwnd,&tmp[0],length);
    return &tmp[0];
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #34
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    If you know the length you can use char arrays safely, just leave one char for the final NULL.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #35
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    Ok, so, if I'm just making sure that there is no way my CHAR is getting overrun and declaring it with enough arrays, it is safe to use?

    Another question, when declaring variables, is it better to always set them to 0 when you declare them?
    Code:
    CHAR Cha [ 512 ];
    
    INT Int;
    
    LPITEMIDLIST ITL;
    Or:
    Code:
    CHAR Cha [ 512 ] = { 0 };
    
    INT Int = 0;
    
    LPITEMIDLIST ITL = NULL;

  6. #36
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if I'm just making sure that there is no way my CHAR is getting overrun and declaring it with enough arrays, it is safe to use?
    Yes, if you use them correctly, they are safe. The point is that they are more difficult to use correctly.

    >> is it better to always set them to 0 when you declare them?
    Yes, always initialize variables. In some rare cases it doesn't matter, but since it doesn't matter, it doesn't hurt to initialize the variable.

    What you should be trying to do is to not declare a variable until you are ready to initialize it with a valid value. In C++ you should keep variables in the smallest possible scope. So if you are initializing a variable to 0 at the top of the function, then changing it to a real value later before you start using it, you should just declare it later when you have that real value available.

  7. #37
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    It is better to set them to zero as C++ will not always do it for you and you could end up with garbage data.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #38
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    Ok, good to know, but there is another issue, windows uses alot of self 'designed' variables, how can I know what kind of data I have to set to some variables?

  9. #39
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you give an example?

  10. #40
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    No problem .

    Code:
    HKEY Key = ???;
    
    HANDLE Han = ???;
    
    HWND Win = ???;
    
    HBITMAP BMP = ???;
    
    HBRUSH Bru = ???;
    
    HDC DCo = ???;
    
    HFILE Fil = ???;
    
    HFONT Fon = ???;
    
    LRESULT Res = ???;
    By the way, sometimes when you declare a variable, you want to give it a value right away.

    INT Int = 20;

    Should you first declare it to zero and then give it his value.

    INT Int = 0;

    Int = 20;

    Or is the first way better?

  11. #41
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The windows API documentation documents these typedefs. The web also surely has tables describing these types.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #42
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> sometimes when you declare a variable, you want to give it a value right away.
    That is my point. You should always try to wait until you are ready to give it a value before you declare it, then use the first option.

    As for the windows types, if you wait to declare them until you are ready to initialize them, it shouldn't be a problem. In the cases where you have a reason to initialize them to a zero-like value, they have corresponding options. Several of those use NULL (which is just 0 anyway), but some use something different like HFILE might be set to INVALID_HANDLE_VALUE. You just have to look in MSDN at the instructions and code examples to find out the appropriate one for each type.

  13. #43
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Usually, I set them to 0, that's the easiest way and is correct for most of the Win32 API data types.

    It is just good to know that they're actually pretty much all int's, they just have different names, so you could understand what purpose one of these variables has.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

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