Thread: Doubt about freeing memory

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    38

    Doubt about freeing memory

    Hello. I'm using this function to concatenate two strings, and return the resulting string.

    Code:
    char * concatenate (char* str1, char* str2)
    {
    	char *result;
    	if ( str2 != NULL )
    	{
    		result = malloc(strlen(str1) + strlen(str2) + 1);
    		if ( result != NULL )
    		{
    			strcpy(result, str1);
    			strcat(result, str2);
    		}
    	}
    	return result;
    }
    My question, Is this function leaking memory?
    If it does, is there another way to do it? I just ran out of ideas.

    Thanks!!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That function in itself doesn't leak memory, but if you don't eventually free the pointer that came back from the function, then you are leaking.

    [And in English you don't say "Doubt about ...", but "Question about ..."]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    Thanks a lot. That's what I thought.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    "I Doubt..." means "I don't believe..." which is a statement, not a question.

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by cpjust View Post
    "I Doubt..." means "I don't believe..." which is a statement, not a question.
    Not necessarily.

    "I am in doubt about how I would go about.. " is a perfectly valid sentence.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by cpjust View Post
    "I Doubt..." means "I don't believe..." which is a statement, not a question.
    No doubt!

    I've a question about matsp's remark... HE states that the pointer returned from concatenate() will be left on the stack. Should main, therefore, be coded such as:

    Code:
    int main(void) 
      char * newstring ; 
      newstring = concatenate("Hello ", "World") ; 
      printf("%s\b",newstring) ; 
      free(newstring) ;  
    }
    ??
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    More thoughts - that would free the new malloc'ed storage - but what about freeing the returned pointer?
    Last edited by Dino; 02-04-2008 at 05:23 PM.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    More thoghts - that would free the new malloc'ed storage - but what about freeing the returned pointer?
    That's exactly what you should free. Obviously, if you do something like this:
    Code:
    char *a, *b, *c;
    a = malloc(10);
    strcpy(a, "Something");
    b = malloc(10);
    strcpy(b, "Otherstr");
    c = concatenate(a, b);
    ...
    free(c);
    would leave a and b allocated but not freed.

    Every malloc should have a matching free somewhere in the code - the number of malloc() and free() occurrences in the source code doesn't have to match, but the calls for malloc() should have a matching call to free() somewhere.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    FWIW, a pointer to pointer might help you keep track of freeing memory where it is allocated, even if you previously allocated something.
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stddef.h> /** for NULL and size_t **/
    
    /** Concatenate string a to string b and store the result **/
    char * concat ( char ** result, const char * a, const char * b )
    {
        char * more = NULL;
        if ( a && b )
        {
            size_t alen = strlen( a );
            size_t blen = strlen( b );
    
            more = realloc( *result, alen + blen + 1 );
            if ( more )
            {
                strcpy( more, a );
                strcat( more, b );
                *result = more;
            }
        }
        return more;
    }
    
    int main ( void )
    {
        char * p = NULL;
       
        if ( concat( &p, "Hello, ", "world." ) )
        {
            puts( p );
            free( p );
        }
        return 0;
    }

  10. #10
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by matsp View Post
    [And in English you don't say "Doubt about ...", but "Question about ..."]

    --
    Mats
    Doubt is synonym of uncertainty.
    If it is grammatically correct to say: Uncertainty about freeing memory, as a tittle, it is correct to write doubt about freeing memory, even when most people confuse doubt with question.
    Last edited by Aia; 02-04-2008 at 05:32 PM.
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing memory for non-pointer variables
    By SuperGodAntMan in forum C++ Programming
    Replies: 7
    Last Post: 02-11-2006, 01:30 AM
  2. Freeing memory before exit
    By tretton in forum C Programming
    Replies: 13
    Last Post: 02-01-2006, 07:33 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. memory allocation and freeing
    By Jase in forum Linux Programming
    Replies: 1
    Last Post: 05-25-2003, 06:26 AM
  5. freeing memory
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 04-27-2003, 08:51 PM