Thread: Possible memory leak?

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92

    Possible memory leak?

    I created a function - primarily to remove quotes - that removes the first and last character from a string if the character sent to the function matches those first and last of the sent string. However, Since I end up incrementing a pointer I was wondering if I had sent the function an allocated array (say a "char String[100]") and the first and last character are removed would trying to access element 99 cause a memory leak?

    Code:
      int Remove_Outer
      (
              char *String,
        const char Outer
      ){
        int Last_Index = strlen(String) - 1;
        if(Last_Index < 0)
          return FAILURE;
        if(String[0] != Outer || String[Last_Index] != Outer)
          return FAILURE;
        String[Last_Index] = '\0';
        String++; /* May cause memory leak here */
        return SUCCESS;
      }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    When unquoting strings you basically have two choices...
    1) Replace the quotation marks with spaces
    2) move the text forward to eliminate the blank leading space.

    You should never simply move the pointers.

    Code:
    void unquote(char *str)
      { char *h = str; // safe head and end pointers
         char *e = str; 
         while (*h)
           { if (*e == '\"')
                e++;
              else
                *(h++) = *(e++); } }
    Last edited by CommonTater; 05-02-2011 at 10:50 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Can I do a shifting operation on the bytes to move the characters over?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    char string[100] is not allocated on heap. there's nothing to leak.
    For heap allocated, as long as you have the pointer to allocated heap. It's not leak.
    Don't forget that incrementing string in your function cannot even modify what the pointer points in caller.

    eg.
    Code:
    void foo( char *s) 
    {
       s += 5;    // assume ok to inc 5
    }
    
    char s[] = "hello world";
    foo(s);
    printf("%s\n",s);       // s still points to "hello world" not " world"

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    Can I do a shifting operation on the bytes to move the characters over?
    See my example in message 2.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > String++; /* May cause memory leak here */
    Well since String is in effect a local variable at this point, incrementing it has NO effect on the caller.

    > Can I do a shifting operation on the bytes to move the characters over?
    Sure, you can do s[n] = s[n+1] in a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Sorry, I am still relatively new to C Tater. If you could add some comments to your example to help me understand it would be very much appreciated.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Sure, you can do s[n] = s[n+1] in a loop.
    And if the string has 3 quotation marks at the beginning or 2 at the end?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    Sorry, I am still relatively new to C Tater. If you could add some comments to your example to help me understand it would be very much appreciated.
    Code:
    void unquote(char *str)
      { char *h = str; // safe head and end pointers
         char *e = str; 
         while (*h)               // exit on trailing null
           { if (*e == '\"')      // if we find a quote
                e++;              // increment the end pointer
              else 
                *(h++) = *(e++); } }  // otherwise copy the character.
    Basically it's setting two pointers to the first part of the string.
    Each time a quotation mark is found we increment the end pointer skipping that character.
    If the character is not a quotation mark we copy it to the head pointer and increment both pointers.
    The loop terminates when we copy the trailing null at the end of the string.

  10. #10
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    And if the string has 3 quotation marks at the beginning or 2 at the end?
    This isn't actually a problem for my specific need, but it is always helpful to study code you don't understand. So I think I am going to use the simple method and shift the characters over.

    Here is my final function:
    Code:
      int Remove_Outer
      (
              char *String,
        const char Outer
      ){
        int Last_Index = strlen(String) - 1;
        if(Last_Index < 0)
          return FAILURE;
        if(String[0] != Outer || String[Last_Index] != Outer)
          return FAILURE;
        String[Last_Index] = '\0';
        for(int i = 1;i <= Last_Index;i++)
          String[i - 1] = String[i];
        return SUCCESS;
      }
    Thanks again for all of the help.

    Edit: Is it possible to use bit shifting to accomplish this as well?

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    This isn't actually a problem for my specific need, but it is always helpful to study code you don't understand. So I think I am going to use the simple method and shift the characters over.

    Here is my final function:
    Code:
      int Remove_Outer
      (
              char *String,
        const char Outer
      ){
        int Last_Index = strlen(String) - 1;
        if(Last_Index < 0)
          return FAILURE;
        if(String[0] != Outer || String[Last_Index] != Outer)
          return FAILURE;
        String[Last_Index] = '\0';
        for(int i = 1;i <= Last_Index;i++)
          String[i - 1] = String[i];
        return SUCCESS;
      }
    Thanks again for all of the help.

    Edit: Is it possible to use bit shifting to accomplish this as well?
    No you won't do this with bit shifting... each charater is in a different byte. What you're doing is byte shifting.

    That said, I really think you should spend some time with your function in a debugger... Frankly, reading it is making me dizzy and you are using Outer in an uninitialized state.

  12. #12
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Quote Originally Posted by CommonTater View Post
    Frankly, reading it is making me dizzy
    Is this a comment on my style?

    Quote Originally Posted by CommonTater View Post
    ... you are using Outer in an uninitialized state.
    What do you mean by using Outer in an "uninitialized state"? Are the negative consequences for this, and what is recommended instead?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    Is this a comment on my style?
    Well, sort of, since it's rather hard to follow what you're doing...


    What do you mean by using Outer in an "uninitialized state"? Are the negative consequences for this, and what is recommended instead?
    Code:
    const char Outer;
    
    // then first use
    if(String[0] != Outer || String[Last_Index] != Outer)
    What is the value of Outer at that moment?

  14. #14
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    It is passed to the function in the call - in the case discussed it would be the \" character. The call would look like this:

    Code:
    Remove_Outer(String, '\"');
    Edit: It first check to see if it the character passed does indeed surround the string, if not it returns FAILURE because there is no removal operation to be done.
    Last edited by 127.0.0.1; 05-02-2011 at 11:40 AM.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    It is passed to the function in the call - in the case discussed it would be the \" character. The call would look like this:

    Code:
    Remove_Outer(String, '\"');
    Edit: It first check to see if it the character passed does indeed surround the string, if not it returns FAILURE because there is no removal operation to be done.
    Ahh... ok, I see that now... it's your unusual text setup... I initially missed the bracketing in your function definition...
    Most often these things are written on a single line or consecutive lines if they are too long... it is exceedingly rare to see a function definition spaced out like that... A far more readable version would be...
    Code:
     int Remove_Outer  ( char *String, const char Outer )
       {
        int Last_Index = strlen(String) - 1;
    
        if(Last_Index < 0)
          return FAILURE;
    
        if(String[0] != Outer || String[Last_Index] != Outer)
          return FAILURE;
    
        String[Last_Index] = '\0';
    
        for(int i = 1;i <= Last_Index;i++)
          String[i - 1] = String[i];
    
        return SUCCESS;
      }
    The compiler may not care a rat's behind about text setup... but we humans sure do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin memory leak?
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2009, 02:38 AM
  2. memory leak
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 11-11-2007, 01:27 PM
  3. Possible Memory Leak?
    By g1i7ch in forum C Programming
    Replies: 12
    Last Post: 05-25-2007, 01:35 PM
  4. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  5. A memory leak (I think...)
    By Stevek in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 03:09 PM