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

    Memory Leak Problem!

    Code:
    char* get(char buffer[],int from, int until, int len)
    {
    int a = from;
    int b = 0;
    int end = from+until;
    char *lol;
    lol = new char[until+1];
    while (a < len && a <= end) {
    lol[b] = buffer[a];
    a++;
    b++;
    }
    lol[b] = '\0';
    return lol;
    }
    That, as you can see, is causing a memory leak since i am using new.
    The problem is i have no idea where to use delete.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Memory Leak Problem!

    After the return of the function.
    Code:
    char *myChar = get (... parameters);
    
    ... do something with myChar.
    
    delete[] myChar;

  3. #3
    Join Date
    Jul 2009
    Posts
    23

    Re: Memory Leak Problem!

    oh i see, thanks!

  4. #4
    Join Date
    Jul 2009
    Posts
    23

    Re: Memory Leak Problem!

    Another problem:

    What happens if you do
    Code:
    printf(get(...para's))

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

    Re: Memory Leak Problem!

    Quote Originally Posted by HouseMD93 View Post
    Another problem:

    What happens if you do
    Code:
    printf(get(...para's))
    A memory leak, and one you cannot fix making calls like that.

    Why not drop using the memory allocation altogether, and return a std::string or even a container (i.e. vector<char>)?

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2009
    Posts
    23

    Re: Memory Leak Problem!

    Will it be ok if I use char instead of the pointer: char*?

    Will that also cause a memory leak?

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

    Re: Memory Leak Problem!

    If you only want to return a single character, that'll work.

    If you're interested in returning an entire string, then you have three basic options:
    1) Allocate in the function and make sure you clean up after yourself.
    2) Return a class type which automates cleanup internally. The most obvious such is std::string.
    3) Make the output string a parameter so that no allocation is necessary. The downside here is that the caller needs to know how big to make the array.

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

    Re: Memory Leak Problem!

    Example:
    Code:
    #include <string>
    std::string get(char buffer[],int from, int until, int len)
    {
        int end = from + until;
        int maxChars = min(len, end);
        return std::string(buffer + from, maxChars);
    }
    Not tested, but this should do the same thing as the code you have now, except that you are actually returning a string.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Jul 2009
    Posts
    23

    Re: Memory Leak Problem!

    Ok thanks, although i have to rewrite every piece of code i have written. Atleast this way i won't get a memory leak.

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

    Re: Memory Leak Problem!

    It's probably better in the long run to rewrite using std::strings anyway.

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