×

Loading...
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!

glad to discuss with you, actually I am confussed now, some compiler's behavior is too strange!!

本文发表在 rolia.net 枫下论坛I supposed the sum should be 2, and aa[] should be in the stack, aa[20] should be equal to the value of variable defined just above it(not below it because of stack), which is pad=2 here. I am pretty sure it happened in my work before.

But unfortunately! I got the different result this morning when I ran it on my new notebook
which is ppc CPU and linux runing on it. the thing become complicated. basically, the value should
be consistant with the compiler, but in this case , the aa[] is separated from other variables which make me confessed now!

thanks for you answer! it's pretty good!

the following is the runing result and disassembly code
display how the compiler allocate the memory for automatic variables.



debian:~/src/tmp$ gcc -g -o tmp tmp.c
debian:~/src/tmp$ ./tmp
sum=0X7ffffb30


debian:~/src/tmp$ gcc -v
Reading specs from /usr/lib/gcc-lib/powerpc-linux/2.95.4/specs
gcc version 2.95.4 20011002 (Debian prerelease)
debian:~/src/tmp$ !objdu
objdump -D -S ./tmp > disa

00004a0: 4e 80 00 20 blr

100004a4 <main>:
#include "stdio.h"
#include "string.h"

main()
{
100004a4: 94 21 ff d0 stwu r1,-48(r1)
100004a8: 7c 08 02 a6 mflr r0
100004ac: 93 41 00 18 stw r26,24(r1)
100004b0: 93 61 00 1c stw r27,28(r1)
100004b4: 93 81 00 20 stw r28,32(r1)
100004b8: 93 a1 00 24 stw r29,36(r1)
100004bc: 93 c1 00 28 stw r30,40(r1)
100004c0: 93 e1 00 2c stw r31,44(r1)
100004c4: 90 01 00 34 stw r0,52(r1)
100004c8: 7c 3f 0b 78 mr r31,r1
int MAX_LEN = 20;
100004cc: 38 00 00 14 li r0,20
100004d0: 90 1f 00 08 stw r0,8(r31)
int pad = 2 ;
100004d4: 38 00 00 02 li r0,2
100004d8: 90 1f 00 0c stw r0,12(r31)
100004dc: 81 3f 00 08 lwz r9,8(r31)
100004e0: 3b a9 ff ff addi r29,r9,-1
100004e4: 3b 9d 00 01 addi r28,r29,1
100004e8: 7f 80 e3 78 mr r0,r28
100004ec: 54 09 28 34 rlwinm r9,r0,5,0,26
100004f0: 7d 3c 4b 78 mr r28,r9
100004f4: 3b 7d 00 01 addi r27,r29,1
100004f8: 7f 60 db 78 mr r0,r27
100004fc: 54 09 10 3a rlwinm r9,r0,2,0,29
10000500: 7d 3b 4b 78 mr r27,r9
10000504: 57 89 e8 fe rlwinm r9,r28,29,3,31
10000508: 39 69 00 0f addi r11,r9,15
1000050c: 38 0b 00 07 addi r0,r11,7
10000510: 54 09 e8 fe rlwinm r9,r0,29,3,31
10000514: 7d 20 4b 78 mr r0,r9
10000518: 54 09 18 38 rlwinm r9,r0,3,0,28
1000051c: 80 01 00 00 lwz r0,0(r1)
10000520: 7d 29 00 d0 neg r9,r9
10000524: 7c 01 49 6e stwux r0,r1,r9
10000528: 39 21 00 08 addi r9,r1,8
1000052c: 38 09 00 0f addi r0,r9,15
10000530: 54 09 e1 3e rlwinm r9,r0,28,4,31
10000534: 7d 20 4b 78 mr r0,r9
10000538: 54 1a 20 36 rlwinm r26,r0,4,0,27
int aa[MAX_LEN] ;
int sum = 0;
1000053c: 38 00 00 00 li r0,0
10000540: 90 1f 00 10 stw r0,16(r31)
int i=20;
10000544: 38 00 00 14 li r0,20
10000548: 90 1f 00 14 stw r0,20(r31)

/*
* just set aa[] to 0 ;
*/
bzero(aa, sizeof(int)*MAX_LEN);
1000054c: 80 1f 00 08 lwz r0,8(r31)
10000550: 7c 09 03 78 mr r9,r0
10000554: 55 20 10 3a rlwinm r0,r9,2,0,29
10000558: 7f 43 d3 78 mr r3,r26
1000055c: 7c 04 03 78 mr r4,r0
10000560: 48 01 02 9d bl 100107fc <__bss_start+0x68>

aa[0] = 0;
10000564: 38 00 00 00 li r0,0
10000568: 90 1a 00 00 stw r0,0(r26)
while (i)
1000056c: 80 1f 00 14 lwz r0,20(r31)
10000570: 2c 00 00 00 cmpwi r0,0
10000574: 40 82 00 08 bne- 1000057c <main+0xd8>
10000578: 48 00 00 34 b 100005ac <main+0x108>
sum +=aa[i--];
1000057c: 39 3f 00 14 addi r9,r31,20
10000580: 81 69 00 00 lwz r11,0(r9)
10000584: 7d 60 5b 78 mr r0,r11
10000588: 7c 0a 03 78 mr r10,r0
1000058c: 55 40 10 3a rlwinm r0,r10,2,0,29
10000590: 81 5f 00 10 lwz r10,16(r31)
10000594: 7c 1a 00 2e lwzx r0,r26,r0
10000598: 7d 4a 02 14 add r10,r10,r0
1000059c: 91 5f 00 10 stw r10,16(r31)
100005a0: 39 6b ff ff addi r11,r11,-1
100005a4: 91 69 00 00 stw r11,0(r9)
100005a8: 4b ff ff c4 b 1000056c <main+0xc8>
printf("sum=0X%x\n",sum);
100005ac: 3d 20 10 00 lis r9,4096
100005b0: 38 69 06 8c addi r3,r9,1676
100005b4: 80 9f 00 10 lwz r4,16(r31)
100005b8: 4c c6 31 82 crclr 4*cr1+eq
100005bc: 48 01 02 39 bl 100107f4 <__bss_start+0x60>
}
100005c0: 81 61 00 00 lwz r11,0(r1)
100005c4: 80 0b 00 04 lwz r0,4(r11)
100005c8: 7c 08 03 a6 mtlr r0
100005cc: 83 4b ff e8 lwz r26,-24(r11)
100005d0: 83 6b ff ec lwz r27,-20(r11)
100005d4: 83 8b ff f0 lwz r28,-16(r11)
100005d8: 83 ab ff f4 lwz r29,-12(r11)
100005dc: 83 cb ff f8 lwz r30,-8(r11)
100005e0: 83 eb ff fc lwz r31,-4(r11)
100005e4: 7d 61 5b 78 mr r1,r11
100005e8: 4e 80 00 20 blr

100005ec <__do_global_ctors_aux>:
100005ec: 94 21 ff f0 stwu r1,-16(r1)
100005f0: 7c 08 02 a6 mflr r0
100005f4: 93 e1 00 0c stw r31,12(r1)
100005f8: 90 01 00 14 stw r0,20(r1)
100005fc: 3d 20 10 01 lis r9,4097
10000600: 80 09 07 70 lwz r0,1904(r9)
10000604: 3b e9 07 70 addi r31,r9,1904
10000608: 2c 00 ff ff cmpwi r0,-1
1000060c: 41 82 00 1c beq- 10000628 <__do_global_ctors_aux+0x3c>
10000610: 81 3f 00 00 lwz r9,0(r31)
10000614: 7d 28 03 a6 mtlr r9
10000618: 4e 80 00 21 blrl
1000061c: 84 1f ff fc lwzu r0,-4(r31)
10000620: 2c 00 ff ff cmpwi r0,-1
10000624: 40 82 ff ec bne+ 10000610 <__do_global_ctors_aux+0x24>
10000628: 80 01 00 14 lwz r0,20(r1)
1000062c: 7c 08 03 a6 mtlr r0
10000630: 83 e1 00 0c lwz r31,12(r1)
10000634: 38 21 00 10 addi r1,r1,16
10000638: 4e 80 00 20 blr更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 问题讨论,考考你的C语言(a lillte bit more than that)知识很有趣哟
    #include "stdio.h"
    #include "string.h"

    main()
    {
    const int MAX_LEN=20;
    int sum = 0;
    int pad=2;
    int aa[MAX_LEN];
    int i=20;

    /*
    * just set aa[] to 0 ;
    */
    bzero(aa, sizeof(int)*MAX_LEN);

    while (i) /* bug here and following line */
    sum +=aa[i--];
    printf("sum=%d\n",sum);
    }
    ~

    question:
    1) can it pass the compilation?

    2) before you fix the bug in the code and got the executalbe file, say tmp or tmp.exe
    what is the result:
    a, 0
    b, 20
    c, 19
    d, 2
    e. unexpected
    ~
    ~
    • The code can pass compilation. The result is e. unexpected
      The root-cause of the bug is the priority of "[]" is greater than "++". That results in sum = aa[20]+aa[19]+...
      To avoid the operator priority error, I would suggest modify the code in simple way:
      {
      i --;
      sum += aa[i];
      }

      aa[MAX_LEN] is acceptable in most popular compliers, but it may not be passed in old C compiler like C80.
      • great! because you mentioned the compiler,But
        what is the value of the aa[20]? is it consistant or just random?
        • aa[20] would be located in the position of the space for int i=20 in the used stack.
          I'm sure its value should be 20 prior to do i--. Therefore, the sum is supposed to be 20.
          • glad to discuss with you, actually I am confussed now, some compiler's behavior is too strange!!
            本文发表在 rolia.net 枫下论坛I supposed the sum should be 2, and aa[] should be in the stack, aa[20] should be equal to the value of variable defined just above it(not below it because of stack), which is pad=2 here. I am pretty sure it happened in my work before.

            But unfortunately! I got the different result this morning when I ran it on my new notebook
            which is ppc CPU and linux runing on it. the thing become complicated. basically, the value should
            be consistant with the compiler, but in this case , the aa[] is separated from other variables which make me confessed now!

            thanks for you answer! it's pretty good!

            the following is the runing result and disassembly code
            display how the compiler allocate the memory for automatic variables.



            debian:~/src/tmp$ gcc -g -o tmp tmp.c
            debian:~/src/tmp$ ./tmp
            sum=0X7ffffb30


            debian:~/src/tmp$ gcc -v
            Reading specs from /usr/lib/gcc-lib/powerpc-linux/2.95.4/specs
            gcc version 2.95.4 20011002 (Debian prerelease)
            debian:~/src/tmp$ !objdu
            objdump -D -S ./tmp > disa

            00004a0: 4e 80 00 20 blr

            100004a4 <main>:
            #include "stdio.h"
            #include "string.h"

            main()
            {
            100004a4: 94 21 ff d0 stwu r1,-48(r1)
            100004a8: 7c 08 02 a6 mflr r0
            100004ac: 93 41 00 18 stw r26,24(r1)
            100004b0: 93 61 00 1c stw r27,28(r1)
            100004b4: 93 81 00 20 stw r28,32(r1)
            100004b8: 93 a1 00 24 stw r29,36(r1)
            100004bc: 93 c1 00 28 stw r30,40(r1)
            100004c0: 93 e1 00 2c stw r31,44(r1)
            100004c4: 90 01 00 34 stw r0,52(r1)
            100004c8: 7c 3f 0b 78 mr r31,r1
            int MAX_LEN = 20;
            100004cc: 38 00 00 14 li r0,20
            100004d0: 90 1f 00 08 stw r0,8(r31)
            int pad = 2 ;
            100004d4: 38 00 00 02 li r0,2
            100004d8: 90 1f 00 0c stw r0,12(r31)
            100004dc: 81 3f 00 08 lwz r9,8(r31)
            100004e0: 3b a9 ff ff addi r29,r9,-1
            100004e4: 3b 9d 00 01 addi r28,r29,1
            100004e8: 7f 80 e3 78 mr r0,r28
            100004ec: 54 09 28 34 rlwinm r9,r0,5,0,26
            100004f0: 7d 3c 4b 78 mr r28,r9
            100004f4: 3b 7d 00 01 addi r27,r29,1
            100004f8: 7f 60 db 78 mr r0,r27
            100004fc: 54 09 10 3a rlwinm r9,r0,2,0,29
            10000500: 7d 3b 4b 78 mr r27,r9
            10000504: 57 89 e8 fe rlwinm r9,r28,29,3,31
            10000508: 39 69 00 0f addi r11,r9,15
            1000050c: 38 0b 00 07 addi r0,r11,7
            10000510: 54 09 e8 fe rlwinm r9,r0,29,3,31
            10000514: 7d 20 4b 78 mr r0,r9
            10000518: 54 09 18 38 rlwinm r9,r0,3,0,28
            1000051c: 80 01 00 00 lwz r0,0(r1)
            10000520: 7d 29 00 d0 neg r9,r9
            10000524: 7c 01 49 6e stwux r0,r1,r9
            10000528: 39 21 00 08 addi r9,r1,8
            1000052c: 38 09 00 0f addi r0,r9,15
            10000530: 54 09 e1 3e rlwinm r9,r0,28,4,31
            10000534: 7d 20 4b 78 mr r0,r9
            10000538: 54 1a 20 36 rlwinm r26,r0,4,0,27
            int aa[MAX_LEN] ;
            int sum = 0;
            1000053c: 38 00 00 00 li r0,0
            10000540: 90 1f 00 10 stw r0,16(r31)
            int i=20;
            10000544: 38 00 00 14 li r0,20
            10000548: 90 1f 00 14 stw r0,20(r31)

            /*
            * just set aa[] to 0 ;
            */
            bzero(aa, sizeof(int)*MAX_LEN);
            1000054c: 80 1f 00 08 lwz r0,8(r31)
            10000550: 7c 09 03 78 mr r9,r0
            10000554: 55 20 10 3a rlwinm r0,r9,2,0,29
            10000558: 7f 43 d3 78 mr r3,r26
            1000055c: 7c 04 03 78 mr r4,r0
            10000560: 48 01 02 9d bl 100107fc <__bss_start+0x68>

            aa[0] = 0;
            10000564: 38 00 00 00 li r0,0
            10000568: 90 1a 00 00 stw r0,0(r26)
            while (i)
            1000056c: 80 1f 00 14 lwz r0,20(r31)
            10000570: 2c 00 00 00 cmpwi r0,0
            10000574: 40 82 00 08 bne- 1000057c <main+0xd8>
            10000578: 48 00 00 34 b 100005ac <main+0x108>
            sum +=aa[i--];
            1000057c: 39 3f 00 14 addi r9,r31,20
            10000580: 81 69 00 00 lwz r11,0(r9)
            10000584: 7d 60 5b 78 mr r0,r11
            10000588: 7c 0a 03 78 mr r10,r0
            1000058c: 55 40 10 3a rlwinm r0,r10,2,0,29
            10000590: 81 5f 00 10 lwz r10,16(r31)
            10000594: 7c 1a 00 2e lwzx r0,r26,r0
            10000598: 7d 4a 02 14 add r10,r10,r0
            1000059c: 91 5f 00 10 stw r10,16(r31)
            100005a0: 39 6b ff ff addi r11,r11,-1
            100005a4: 91 69 00 00 stw r11,0(r9)
            100005a8: 4b ff ff c4 b 1000056c <main+0xc8>
            printf("sum=0X%x\n",sum);
            100005ac: 3d 20 10 00 lis r9,4096
            100005b0: 38 69 06 8c addi r3,r9,1676
            100005b4: 80 9f 00 10 lwz r4,16(r31)
            100005b8: 4c c6 31 82 crclr 4*cr1+eq
            100005bc: 48 01 02 39 bl 100107f4 <__bss_start+0x60>
            }
            100005c0: 81 61 00 00 lwz r11,0(r1)
            100005c4: 80 0b 00 04 lwz r0,4(r11)
            100005c8: 7c 08 03 a6 mtlr r0
            100005cc: 83 4b ff e8 lwz r26,-24(r11)
            100005d0: 83 6b ff ec lwz r27,-20(r11)
            100005d4: 83 8b ff f0 lwz r28,-16(r11)
            100005d8: 83 ab ff f4 lwz r29,-12(r11)
            100005dc: 83 cb ff f8 lwz r30,-8(r11)
            100005e0: 83 eb ff fc lwz r31,-4(r11)
            100005e4: 7d 61 5b 78 mr r1,r11
            100005e8: 4e 80 00 20 blr

            100005ec <__do_global_ctors_aux>:
            100005ec: 94 21 ff f0 stwu r1,-16(r1)
            100005f0: 7c 08 02 a6 mflr r0
            100005f4: 93 e1 00 0c stw r31,12(r1)
            100005f8: 90 01 00 14 stw r0,20(r1)
            100005fc: 3d 20 10 01 lis r9,4097
            10000600: 80 09 07 70 lwz r0,1904(r9)
            10000604: 3b e9 07 70 addi r31,r9,1904
            10000608: 2c 00 ff ff cmpwi r0,-1
            1000060c: 41 82 00 1c beq- 10000628 <__do_global_ctors_aux+0x3c>
            10000610: 81 3f 00 00 lwz r9,0(r31)
            10000614: 7d 28 03 a6 mtlr r9
            10000618: 4e 80 00 21 blrl
            1000061c: 84 1f ff fc lwzu r0,-4(r31)
            10000620: 2c 00 ff ff cmpwi r0,-1
            10000624: 40 82 ff ec bne+ 10000610 <__do_global_ctors_aux+0x24>
            10000628: 80 01 00 14 lwz r0,20(r1)
            1000062c: 7c 08 03 a6 mtlr r0
            10000630: 83 e1 00 0c lwz r31,12(r1)
            10000634: 38 21 00 10 addi r1,r1,16
            10000638: 4e 80 00 20 blr更多精彩文章及讨论,请光临枫下论坛 rolia.net
            • exactly. your outcome is correct. I've confirmed it in Borland C++.
              As to BC compiler, the stack map is as follow:
              low addr: i = 20
              aa[0]...[20]
              pad = 2
              sum = 0
              return address
              high addr ...

              aa[20]'s location in memory points to the space of pad. That's why the sum should be 2 all the time.

              However, the result depends on the C compiler for the location of aa[20].
              • oh, really? shakehand!
    • 大家平时是靠调试找出这些错误呢?还是光看源码就行了,这就太牛了!
      • experience pus debuging
      • 有什么意义啊! 30岁以后还有为代码技巧和识别编译错误自豪的吗?
        • just technical discussion , no thing more, OK?
          • OK,OK,OK.我跟M30说话呢,我不掺和你的technical discussion .
            • 误会,误会,原来你们认识, 接着骂,接着骂。。
              我的意思是”只是技术讨论而已(当然是低级技术),没有自豪的意思,是吗!“, 言不达意,见谅,见谅!
              • 我根本不认识他啊
                • To m30:
                  from you old posts, I found you graduated form an U. in wuhan, probably we know each other. I came from wuhan (HUST when 6.4)
                  • 啊,有人在暗中收集我的信息啊?可怕!HUST too。不过到此为止,别再追问细节了啊?见同学情更怯啊!
                    • sorry! :-(
                      • 看来以后我要做个好人了,这儿还有同学在盯着呢!
                        • 我算是知道了什么是“自作多情”
                          兄弟,没事,接着当坏人。
                          噢,别忘了,下次丢人显眼的时候,别说是HUST的 。。。就说你是。。。。武大的 :P
                          • .
                            呸,你一肚子坏水。偶看见就宣传他是HUST的。:))
                            • :-(
                              别台举他呀,大姐,?啊! 我知道你的难处了,下回就说你没读过书不就得了。 :-(
                              • :-(
                                说偶没读过书倒是真的。
                                • don't be sad, lady.just kidding LA
                            • 我知道你是UofW的,我们都不要互相结发了,好吧?
        • 正中我的痛处
    • If you folks are genuinely up to improve your C skills. Go get a book by Sun Microsystems' Peter Van Der Linden, you'll be amazed how deep the C world is.
      • Peter's web site.
      • Sample question from the book: why Halloween = Christmas?
        • My trouble is "why Halloween !=Christmas" see #1240400