El Monday 04 August 2008 01:01:56 Alan Liu escribió:
> Hi All,
>
> I am using uClibc(GCC 4.2.3) for development on MIPS R3000 platform.
> And I found one optimization bug using the following C codes:
> ---------------------------------------------------------------------------
>------------------------------ #include<stdio.h>
>
> typedef struct list{
>     struct list *left;
>     struct list *right;
> }list_t;
> list_t a;
>
> int test()
> {
>     a.left = 0x12345678;
> }
>
> int fun1()
> {
>     unsigned  int c = 1;
>
>     c = *(int  *)(&a.left);
>     a.left = &a;
>     printf("fun1:c:%x  a.left:%x    &a:%x\n", c,a.left,&a);
>     return 0;
>
> }
> int fun2()
> {
>     unsigned  int c = 1;
>
>     c = *(int  *)(&a.left);
>     a.left = &a;
>
> printf("fun2:-----------------\n");
>
>     printf("fun2:c:%x  a.left:%x    &a:%x\n", c,a.left,&a);
>     return 0;
>
> }
>
> main()
> {
> test();
> fun1();
> fun2();
> }
> ---------------------------------------------------------------------------
>------------------------ The console output after running the executable
> check(mips-linux-gcc -O2 -o check check.c) is:
>
> fun1:c:12345678  a.left:4409b0  &a:4409b0
> fun2:-----------------
> fun2:c:4409b0  a.left:4409b0    &a:4409b0
>
> It seems fun2() prints wrong data of variable c.
>

It prints the right value. See why:

First, in test, you assign to *(int)(&a.left) the address 0x12345678
Then, in fun1, you assign to *(int*)(&a.left) the address of a.
After that, in fun2, you assign to c the value stored in a.left

That's why you got 4409b0 (the address of a) in c. Remember: a is a global 
variable.

> Who could help ?
>
> Thanks,
> Alan

-- 
Francisco Castro

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
uClibc mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/uclibc

Reply via email to