Re: g_strstrip question.



--- Brett Nash <nash nash nu> wrote:
  If so, how do I free the whole string so that I do not get memory leaks ?

        If you have some string str1 which you need to free...

        char *str1;
        char *oldstr;

        /* Replace this with however str1 is allocated */
        str1 = g_strdup("Some random string");
        
        /* Save a copy */
        oldstr = str1;
        
        str1 = g_strstrip(str1);
        ...

        g_free(oldstr); /* str1 is now an invalid pointer */
        str1 = oldstr = NULL; /* sanity */
        
        Regards,
        nash

-- 
Brett Nash <nash nash nu>
Sometimes it's better to light a flamethrower than curse the darkness.
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


  This makes more sense -- oldptr has info on how many bytes to free up, while
the newptr only navigates the bytes comprising the string.

 I have changed the code to do that.

 Thanks for the reply and help.

Harring.

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]