Re: g_strstrip question.



Tristan Van Berkom wrote:

Geez this has gone on long enough.

first of all you cant just free any memory address.
Yes that makes sence.

char *str = g_strdup(...);

--> g_free(str);

is good

--> g_free(str[1]);

is worse than a memory leek (ie. the memory manager
never allocated memory for that address)
Who said anything about freeing from the middle of the gchar *?

the same goes for the rest of the string.
(i.e if the trailing NULL charachter is no
longer the last allocated address; the _whole_
memory "chunk" will be freed)
what? assume it is now reduced in size, how does it know where the end is, if the trailing NULL character is not the last allocated address? and if there is no NULL character, how does it know how big the chunk is to free it?


second of all

strchug, strchop, strstrip
_all_ modify the string _directly_
OK. if they modify the gchar* DIRECTLY then there would have to be memory left allocated, unless it just sets the return value as a pointer to the first character 'n' along where it feels that is the start of the string which satisfies the function. But if that was true, how would it terminate if there were characters to chop on the end? NULL terminator? Well then when you freed it, (the original or the new pointer) it would not free the WHOLE chunk of memory, it would look for the NULL terminator and free up to that.

NO memory has been allocated.

if you want a "chopped" duplicate you need to:
Yes, ofcourse.

str2 = g_strchop(g_strdup(str1));

otherwise str1 is modified.

If the same memory is used and the end is NULL terminated, then you have a memory leak. Even if you free the original string, it will look for '\0' and free up to that point. Without allocating NEW memory, I can not see how you could do this?

Cheers,
                        -Tristan


Martyn Russell wrote:
I think you'll find that 'b' is a new piece of memory and so is 'c',
therefore, you were right the first time in stating each variable would
have to be freed.  I am curious about one thing though, if g_strstrip is
a DEFINITION of a combination of g_strchug and g_strchop, that in it's
self should be a memory leak shouldn't it?

   gchar *a = NULL;
   gchar *b = NULL;
   gchar *c = NULL;

   a = g_strdup("   mystring   ");
   b = g_strchug(a);
   c = g_strchop(b);

   /* WHERE c is now the string u want */

   if(a != NULL) g_free(a);
   if(b != NULL) g_free(b);
   if(c != NULL) g_free(c);

Martyn

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list







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