GList strange behavior

2012-08-14 Thread Vlasov Vitaly
Hi, list.
I can't understand, why g_list_append return different values in my 
callback's(please read source). In first case i got same list, which was 
obtained from arguments. In second case g_list_append returns newly created 
list.
Why result's is are not the same?

In real program, i need to get newly created list pointer in function like 
button_cb(), but don't know how to do it.

source: http://pastebin.com/4gMREKwd

output on my laptop:
INIT list prt: 0x830f080
Created: 0x830f080, arg: 0x830f080
Last list element: 0x832c610
Init list ptr: 0x832c5c0
Second list ptr: 0x830f120
End
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GList strange behavior

2012-08-14 Thread Ardhan Madras
Hi,

> In button_cb()...
Did you mean Last list element: 0x832c610 is differ from INIT list
prt: 0x830f080?

It's clear because  0x830f080 was the location of first list and
0x832c610 is the last appended list.
g_list_prepend() return to the start of the list, g_list_last() return
the last of the list (address).

> in test_glist()
First you call temp = g_list_insert(test, ...), test = NULL, save the
list to temp pointer to GList,
second, you call temp = g_list_append(test, ...), nothing wrong with
that code! BUT if you suppose to insert 2nd list here, then you are
wrong. Because, test is STILL NULL. It returns the newly allocated
list again, that's why it's differ. You should call call temp =
g_list_append(temp,...) or to insert the next list.

> In real program, i need to get newly created list pointer in function like
> button_cb(), but don't know how to do it.
Just pass it through callback's argument or declare it as global variable.


On Wed, Aug 15, 2012 at 8:04 AM, Vlasov Vitaly  wrote:
> Hi, list.
> I can't understand, why g_list_append return different values in my
> callback's(please read source). In first case i got same list, which was
> obtained from arguments. In second case g_list_append returns newly created
> list.
> Why result's is are not the same?
>
> In real program, i need to get newly created list pointer in function like
> button_cb(), but don't know how to do it.
>
> source: http://pastebin.com/4gMREKwd
>
> output on my laptop:
> INIT list prt: 0x830f080
> Created: 0x830f080, arg: 0x830f080
> Last list element: 0x832c610
> Init list ptr: 0x832c5c0
> Second list ptr: 0x830f120
> End
> ___
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GList strange behavior

2012-08-15 Thread Vlasov Vitaly
Hello
Thanks for answer.

> Did you mean Last list element: 0x832c610 is differ from INIT list
> prt: 0x830f080?
 No.

Lest's see...
i got function "do_work" with:
1. new Glist init with random data
2. callback definition of button click: button_click(, GList *list)
let init list poiner = 0xfe
button was clicked!
Here button_click(GList *list)
list in agrs = 0xfe
new data record memory allocation.
!!! g_list_append(data, list) !!!
In my example it returns init list value (0xfe), but should returns 
value 
of newly created list (0x832c610 in example).

In example:
INIT list prt: 0x830f080 - this is init list, which passed to 
button_callback
In button callback:
Created: 0x830f080, arg: 0x830f080 - first ptr is returned list 
from 
g_list_append(), but it equal to init list. WTF??? HOW?? I need newly created 
list here...
And i check added value with g_list_last(). It's equal 
0x832c610 and 
correct.

So, why g_list_append() dont' return me newly created list???

В сообщении от Среда 15 августа 2012 09:33:04 вы написали:
> Hi,
> 
> > In button_cb()...
> 
> Did you mean Last list element: 0x832c610 is differ from INIT list
> prt: 0x830f080?
> 
> It's clear because  0x830f080 was the location of first list and
> 0x832c610 is the last appended list.
> g_list_prepend() return to the start of the list, g_list_last() return
> the last of the list (address).
> 
> > in test_glist()
> 
> First you call temp = g_list_insert(test, ...), test = NULL, save the
> list to temp pointer to GList,
> second, you call temp = g_list_append(test, ...), nothing wrong with
> that code! BUT if you suppose to insert 2nd list here, then you are
> wrong. Because, test is STILL NULL. It returns the newly allocated
> list again, that's why it's differ. You should call call temp =
> g_list_append(temp,...) or to insert the next list.
> 
> > In real program, i need to get newly created list pointer in function
> > like button_cb(), but don't know how to do it.
> 
> Just pass it through callback's argument or declare it as global variable.
> 
> On Wed, Aug 15, 2012 at 8:04 AM, Vlasov Vitaly  wrote:
> > Hi, list.
> > 
> > I can't understand, why g_list_append return different values in
> > my
> > 
> > callback's(please read source). In first case i got same list, which was
> > obtained from arguments. In second case g_list_append returns newly
> > created list.
> > Why result's is are not the same?
> > 
> > In real program, i need to get newly created list pointer in function
> > like button_cb(), but don't know how to do it.
> > 
> > source: http://pastebin.com/4gMREKwd
> > 
> > output on my laptop:
> > INIT list prt: 0x830f080
> > Created: 0x830f080, arg: 0x830f080
> > Last list element: 0x832c610
> > Init list ptr: 0x832c5c0
> > Second list ptr: 0x830f120
> > End
> > ___
> > gtk-app-devel-list mailing list
> > gtk-app-devel-list@gnome.org
> > https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: GList strange behavior

2012-08-16 Thread Tadej Borovšak
Hi.

Not sure what is troubling you here. GList is represented by pointer
to first element or NULL if list is empty. And when you append items
to the list, the address of the list stays the same, since first
element of the list is unchanged (exception to this is when append
first element to an empty list).

Some code to explain some things:

GList *list = NULL /* Empty list */
list = g_list_append (list, GINT_TO_POINTER (1)); /* Add first
element, list points to some ADDR1 */
list = g_list_append (list, GINT_TO_POINTER (2)); /* Add second
element, list still points to ADDR1 */
...

Cheers,
Tadej

-- 
Tadej Borovšak
blog.borovsak.si
tadeb...@gmail.com
tadej.borov...@gmail.com
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: GList strange behavior

2012-08-16 Thread Vlasov Vitaly
Thank you! I almost understand bahavior of GList.

I need to save newly created list poiner. So, i need to use g_list_last() to 
save it? Is there another method to save created list pointer?

В сообщении от Четверг 16 августа 2012 15:51:22 вы написали:
> Hi.
> 
> Not sure what is troubling you here. GList is represented by pointer
> to first element or NULL if list is empty. And when you append items
> to the list, the address of the list stays the same, since first
> element of the list is unchanged (exception to this is when append
> first element to an empty list).
> 
> Some code to explain some things:
> 
> GList *list = NULL /* Empty list */
> list = g_list_append (list, GINT_TO_POINTER (1)); /* Add first
> element, list points to some ADDR1 */
> list = g_list_append (list, GINT_TO_POINTER (2)); /* Add second
> element, list still points to ADDR1 */
> ...
> 
> Cheers,
> Tadej
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: GList strange behavior

2012-08-16 Thread David Nečas
On Thu, Aug 16, 2012 at 06:49:25PM +0400, Vlasov Vitaly wrote:
> I need to save newly created list poiner.

No.

Many operations change the list head (by adding, removing, reordering,
..., elements).  In general, if an operation returns the new list head
you *must* save and use that in subsequent operations.

>  So, i need to use g_list_last() to 
> save it? Is there another method to save created list pointer?

*Every* operation that may change the list head returns the new list
head.  That's the pointer to use.

Yeti

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GList strange behavior

2012-08-16 Thread Ardhan Madras
Hi,

Seem you did not read my reply clearly. I told you the behaviour of
g_list_append() and g_list_last() there.
Okay, so your question is (

"So, why g_list_append() dont' return me newly created list???"

If you pass NULL pointer to first arg of g_list_append() it will
returns a newly created list for you, save the address to the return
pointer. If you pass non NULL pointer (already created list) to first
arg of g_list_append() it will ALWAYS return the HEAD of the list and
will not create a new list.

It's better for you to read GList implementation source code.
(glist.c) to fully understand how it work.

Regards.


On Thu, Aug 16, 2012 at 2:08 AM, Vlasov Vitaly  wrote:
> Hello
> Thanks for answer.
>
> > Did you mean Last list element: 0x832c610 is differ from INIT list
>> prt: 0x830f080?
>  No.
>
> Lest's see...
> i got function "do_work" with:
> 1. new Glist init with random data
> 2. callback definition of button click: button_click(, GList 
> *list)
> let init list poiner = 0xfe
> button was clicked!
> Here button_click(GList *list)
> list in agrs = 0xfe
> new data record memory allocation.
> !!! g_list_append(data, list) !!!
> In my example it returns init list value (0xfe), but should returns 
> value
> of newly created list (0x832c610 in example).
>
> In example:
> INIT list prt: 0x830f080 - this is init list, which passed to
> button_callback
> In button callback:
> Created: 0x830f080, arg: 0x830f080 - first ptr is returned 
> list from
> g_list_append(), but it equal to init list. WTF??? HOW?? I need newly created
> list here...
> And i check added value with g_list_last(). It's equal 
> 0x832c610 and
> correct.
>
> So, why g_list_append() dont' return me newly created list???
>
> В сообщении от Среда 15 августа 2012 09:33:04 вы написали:
>> Hi,
>>
>> > In button_cb()...
>>
>> Did you mean Last list element: 0x832c610 is differ from INIT list
>> prt: 0x830f080?
>>
>> It's clear because  0x830f080 was the location of first list and
>> 0x832c610 is the last appended list.
>> g_list_prepend() return to the start of the list, g_list_last() return
>> the last of the list (address).
>>
>> > in test_glist()
>>
>> First you call temp = g_list_insert(test, ...), test = NULL, save the
>> list to temp pointer to GList,
>> second, you call temp = g_list_append(test, ...), nothing wrong with
>> that code! BUT if you suppose to insert 2nd list here, then you are
>> wrong. Because, test is STILL NULL. It returns the newly allocated
>> list again, that's why it's differ. You should call call temp =
>> g_list_append(temp,...) or to insert the next list.
>>
>> > In real program, i need to get newly created list pointer in function
>> > like button_cb(), but don't know how to do it.
>>
>> Just pass it through callback's argument or declare it as global variable.
>>
>> On Wed, Aug 15, 2012 at 8:04 AM, Vlasov Vitaly  wrote:
>> > Hi, list.
>> >
>> > I can't understand, why g_list_append return different values in
>> > my
>> >
>> > callback's(please read source). In first case i got same list, which was
>> > obtained from arguments. In second case g_list_append returns newly
>> > created list.
>> > Why result's is are not the same?
>> >
>> > In real program, i need to get newly created list pointer in function
>> > like button_cb(), but don't know how to do it.
>> >
>> > source: http://pastebin.com/4gMREKwd
>> >
>> > output on my laptop:
>> > INIT list prt: 0x830f080
>> > Created: 0x830f080, arg: 0x830f080
>> > Last list element: 0x832c610
>> > Init list ptr: 0x832c5c0
>> > Second list ptr: 0x830f120
>> > End
>> > ___
>> > gtk-app-devel-list mailing list
>> > gtk-app-devel-list@gnome.org
>> > https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list