Why there's still ONE element left after g_slist_free () ?
I created a GSList and then free it using g_slist_free (), then I call g_slist_length() and it return 1, surprising! I thought after g_slist_free (), the GSList should be empty(NULL), why still 1 left? ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Why there's still ONE element left after g_slist_free () ?
On Wed, May 13, 2009 at 02:34:35PM +0800, PenT wrote: > I created a GSList and then free it using g_slist_free (), then I call > g_slist_length() and it return 1, surprising! > > I thought after g_slist_free (), the GSList should be empty(NULL), why still > 1 left? Stop using the invalid pointer that points to the memory freed by g_slist_free() and your problem will be gone. There is no way g_slist_free() could change your pointer -- it does not take a reference to it -- it just frees the list. Yeti ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Why there's still ONE element left after g_slist_free () ?
> I thought after g_slist_free (), the GSList should be empty(NULL), why still When you say "the GSList should be NULL", what do you mean? You have a GSList* variable that used to point to the list? Do you assign NULL to that variable yourself? If not, it is not going to become NULL magically by itself. After the call to g_slist_free(), the variable just points to freed memory in the heap and should not be used. This is not any different than complaining that after char *p = malloc (42); free (p); p is not NULL and the C library must be buggy. --tml ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Why there's still ONE element left after g_slist_free () ?
If you give a pointer to g_slist_length() which is not null the result must be at least 1, because only the null value shows the end of the list. The g_slist_free function does not set it's parameter to null (as it can not do it) so your GSList * variable after the function call points to a freed (maybe reallocated) memory segment. Depending on the content of the pointed memory (especially the next member of the GSList struct) the return value of the g_slist_length will be 1 or greater. The surprise is the result not a segmentation fault. :) PenT wrote: I created a GSList and then free it using g_slist_free (), then I call g_slist_length() and it return 1, surprising! I thought after g_slist_free (), the GSList should be empty(NULL), why still 1 left? ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Why there's still ONE element left after g_slist_free () ?
Thanks to Yeti, tml and Pfeiffer, I finally realized what happend to the GSList. May be I'm spoiled by Python, Java that I had the strange SHOULD-BE-NULL thought, now I know if I need to reuse the GSList* variable, the first thing is to assign NULL to it after g_slist_free (), also surprised g_slist_length() didn't gave a segmentation fault to me, aha. ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Why there's still ONE element left after g_slist_free () ?
american.communist.pa...@gmail.com wrote: > Its old hat to C programmers that you set any object to NULL when > you're done with it, which returns the memory used to the heap. That's specifically /not/ true in C: You have to explicitly free allocated space--setting a pointer to null doesn't automatically do the freeing and, in fact, can simply lose the pointer making it impossible to subsequently free the space. Sometimes it's useful to set the pointer to null /after/ you free the space just as a flag to yourself that you've freed it. > That's not necessary with languages that have garbage collection like > Java, Python, etc. > > On May 13, 2009 3:56am, PenT wrote: > > Thanks to Yeti, tml and Pfeiffer, I finally realized what happend to > the GSList. > > > > May be I'm spoiled by Python, Java that I had the strange > SHOULD-BE-NULL thought, now I know if I need to reuse the GSList* > variable, the first thing is to assign NULL to it after g_slist_free > (), also surprised g_slist_length() didn't gave a segmentation fault > to me, aha. > > > > > ___ > gtk-list mailing list > gtk-list@gnome.org > http://mail.gnome.org/mailman/listinfo/gtk-list > ** ** ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Why there's still ONE element left after g_slist_free () ?
On Thu, 14 May 2009 20:27:18 + american.communist.pa...@gmail.com wrote: > You should quote the object of the comment ENTIRELY before commenting, > shouldn't you? Nobody misquoted you, nor did they strip any of your comment out. And nobody was objecting to your comment as a result of lack of context. > The actual comment by PenT does state as much: > "to assign NULL to it after g_slist_free ()..." Yes, that's true, PenT did state the right thing. > AFTER g_slist_free. I don't see that I was mutually exclusive in my reply. Well how about this: > On May 14, 2009 11:51am, Chris Moller wrote: > > american.communist.pa...@gmail.com wrote: > > > > Its old hat to C programmers that you set any object to NULL >>> when you're done with it, > > > which returns the memory used to the heap. Now note that the "when you're done with it" is merely a contextual clause and can therefore be eliminated from the sentence without significantly changing its meaning (simple English grammar 101). Therefore what you wrote was a statement that setting "any object" to NULL "returns the memory to the heap". This is explicitly wrong. Your sentence should have read: It's old hat to C programmers that you set any pointer to NULL when you're done with it and have returned the memory used by the object it referenced to the heap. Though even that isn't strictly correct since the pointer may not reference a heap object, so you should have written: It's old hat to C programmers that you set any pointer to NULL when you're done with it; especially if you have deleted the object it referenced and returned the memory used to the heap. Sorry for the English lesson, but there are enough people reading this who don't _know_ what you must have meant that it really _does_ matter when you actually _say_ something quite different. HTH Rob ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Re: Why there's still ONE element left after g_slist_free () ?
Its old hat to C programmers that you set any object to NULL when you're done with it, which returns the memory used to the heap. That's not necessary with languages that have garbage collection like Java, Python, etc. On May 13, 2009 3:56am, PenT wrote: Thanks to Yeti, tml and Pfeiffer, I finally realized what happend to the GSList. May be I'm spoiled by Python, Java that I had the strange SHOULD-BE-NULL thought, now I know if I need to reuse the GSList* variable, the first thing is to assign NULL to it after g_slist_free (), also surprised g_slist_length() didn't gave a segmentation fault to me, aha. ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Re: Why there's still ONE element left after g_slist_free () ?
> Its old hat to C programmers that you set any object to NULL when you're done > with it, > which returns the memory used to the heap. That's not necessary with > languages that > have garbage collection like Java, Python, etc. Yes and no. One could say that on the contrary, in languages with garbage collection the only way to (implicitly, eventually) "free" something is to make sure there are no pointers to it, i.e. set the pointers to NULL or to point to something else. There are no explicit "free" calls. (Of course, such languages generally don't use the term "pointer".) --tml ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list
Re: Re: Why there's still ONE element left after g_slist_free () ?
You should quote the object of the comment ENTIRELY before commenting, shouldn't you? The actual comment by PenT does state as much: "to assign NULL to it after g_slist_free ()..." AFTER g_slist_free. I don't see that I was mutually exclusive in my reply. On May 14, 2009 11:51am, Chris Moller wrote: american.communist.pa...@gmail.com wrote: > Its old hat to C programmers that you set any object to NULL when > you're done with it, which returns the memory used to the heap. That's specifically /not/ true in C: You have to explicitly free allocated space--setting a pointer to null doesn't automatically do the freeing and, in fact, can simply lose the pointer making it impossible to subsequently free the space. Sometimes it's useful to set the pointer to null /after/ you free the space just as a flag to yourself that you've freed it. > That's not necessary with languages that have garbage collection like > Java, Python, etc. > > On May 13, 2009 3:56am, PenT pen...@gmail.com> wrote: > > Thanks to Yeti, tml and Pfeiffer, I finally realized what happend to > the GSList. > > > > May be I'm spoiled by Python, Java that I had the strange > SHOULD-BE-NULL thought, now I know if I need to reuse the GSList* > variable, the first thing is to assign NULL to it after g_slist_free > (), also surprised g_slist_length() didn't gave a segmentation fault > to me, aha. > > > > > ___ > gtk-list mailing list > gtk-list@gnome.org > http://mail.gnome.org/mailman/listinfo/gtk-list > ** ** ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list ___ gtk-list mailing list gtk-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-list