在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > Linux下C應用程序開發(fā)

            Linux下C應用程序開發(fā)

            作者: 時間:2011-05-20 來源:網(wǎng)絡 收藏
            gdb 舉例
              本節(jié)用一個實例教你一步步的用 gdb 調(diào)試程序. 被調(diào)試的程序相當?shù)暮唵? 但它展示了 gdb 的典型.
              下面列出了將被調(diào)試的程序. 這個程序被稱為 hello , 它顯示一個簡單的問候, 再用反序將它列出.
              #include stdio.h>;
              static void my_print (char *);
              static void my_print2 (char *);
              main ()
              {
              char my_string[] = hello world!;
              my_print (my_string);
              my_print2 (my_string);
              }
              void my_print (char *string)
              {
              printf (The string is %s , string);
              }
              void my_print2 (char *string)
              {
              char *string2;
              int size, i;
              size = strlen (string);
              string2 = (char *) malloc (size + 1);
              for (i = 0; i size; i++)
              string2[size - i] = string;
              string2[size+1] = '';
              printf (The string printed backward is %s , string2);
              }
              用下面的命令編譯它:
              gcc -g -o hello hello.c
              這個程序執(zhí)行時顯示如下結果:
              ../hello
              The string is hello world!
              The string printed backward is
              輸出的第一行是正確的, 但第二行打印出的東西并不是我們所期望的. 我們所設想的輸出
              應該是:
              The string printed backward is !dlrow olleh
              由于某些原因, my_print2 函數(shù)沒有正常工作. 讓我們用 gdb 看看問題究竟出在哪兒,
              先鍵入如下命令:
              gdb hello
            ------------------------------------------------------------------------------
            --
            注意: 記得在編譯 hello 程序時把調(diào)試選項打開.
            ------------------------------------------------------------------------------
            --
              如果你在輸入命令時忘了把要調(diào)試的程序作為參數(shù)傳給 gdb , 你可以在 gdb 提示符下用
              file 命令來載入它:
              (gdb) file hello
              這個命令將載入 hello 可執(zhí)行文件就象你在 gdb 命令行里裝入它一樣.
              這時你能用 gdb 的 run 命令來運行 hello 了. 當它在 gdb 里被運行后結果大約會象這樣:
              (gdb) run
              Starting program: /root/hello
              The string is hello world!
              The string printed backward is
              Program exited with code 040
              這個輸出和在 gdb 外面運行的結果一樣. 問題是, 為什么反序打印沒有工作? 為了找出癥結所在, 我們可以在 my_print2 函數(shù)的 for 語句后設一個斷點, 具體的做法是在 gdb
            提示符下鍵入 list 命令三次, 列出源代碼:
              (gdb) list
              (gdb) list
              (gdb) list
            ------------------------------------------------------------------------------
            --
            技巧: 在 gdb 提示符下按回車健將重復上一個命令.
            ------------------------------------------------------------------------------
            --
              第一次鍵入 list 命令的輸出如下:
              1 #include stdio.h>;
              2
              3 static void my_print (char *);
              4 static void my_print2 (char *);
              5
              6 main ()
              7 {
              8 char my_string[] = hello world!;
              9 my_print (my_string);
              10 my_print2 (my_string);
            如果按下回車, gdb 將再執(zhí)行一次 list 命令, 給出下列輸出:
              11 }
              12
              13 void my_print (char *string)
              14 {
              15 printf (The string is %s , string);
              16 }
              17
              18 void my_print2 (char *string)
              19 {
              20 char *string2;
              再按一次回車將列出 hello 程序的剩余部分:
              21 int size, i;
              22
              23 size = strlen (string);
              24 string2 = (char *) malloc (size + 1);
              25 for (i = 0; i size; i++)
              26 string2[size - i] = string;
              27 string2[size+1] = '';
              28
              29 printf (The string printed backward is %s , string2);
              30 }
              根據(jù)列出的源程序, 你能看到要設斷點的地方在第26行, 在 gdb 命令行提示符下鍵入如下命令設置斷點:
              (gdb) break 26
              gdb 將作出如下的響應:
              Breakpoint 1 at 0x804857c: file hello.c, line 26.
              (gdb)
              現(xiàn)在再鍵入 run 命令, 將產(chǎn)生如下的輸出:
              Starting program: /root/hello
              The string is hello world!
              Breakpoint 1, my_print2 (string=0xbffffab0 hello world!) at hello.c:26
              26 string2[size - i] = string;
              你能通過設置一個觀察 string2[size - i] 變量的值的觀察點來看出錯誤是怎樣產(chǎn)生的,
              做法是鍵入:
              (gdb) watch string2[size - i]
              gdb 將作出如下回應:
              Hardware watchpoint 2: string2[size - i]
              現(xiàn)在可以用 next 命令來一步步的執(zhí)行 for 循環(huán)了:
              (gdb) next
              經(jīng)過第一次循環(huán)后, gdb 告訴我們 string2[size - i] 的值是 `h`. gdb 用如下的顯示來告訴你這個信息:
              Hardware watchpoint 2: string2[size - i]
              Old value = 0 '00'
              New value = 104 'h'
              my_print2 (string=0xbffffab0 hello world!) at hello.c:25
              25 for (i = 0; i size; i++)
              這個值正是期望的. 后來的數(shù)次循環(huán)的結果都是正確的. 當 i=11 時, 表達式
              string2[size - i] 的值等于 `!`, size - i 的值等于 1, 最后一個字符已經(jīng)拷到新串里了.
              如果你再把循環(huán)執(zhí)行下去, 你會看到已經(jīng)沒有值分配給 string2[0] 了, 而它是新串的第一個字符, 因為 malloc 函數(shù)在分配內(nèi)存時把它們初始化為空(null)字符. 所以 string2 的第一個字符是空字符. 這解釋了為什么在打印 string2 時沒有任何輸出了.
              現(xiàn)在找出了問題出在哪里, 修正這個錯誤是很容易的. 你得把代碼里寫入 string2 的第一個字符的的偏移量改為 size - 1 而不是 size. 這是因為 string2 的大小為 12, 但起始偏移量是 0, 串內(nèi)的字符從偏移量 0 到 偏移量 10, 偏移量 11 為空字符保留.
              改正方法非常簡單. 這是這種解決辦法的代碼:
              #include stdio.h>;
              static void my_print (char *);
              static void my_print2 (char *);
              main ()
              {
              char my_string[] = hello world!;
              my_print (my_string);
              my_print2 (my_string);
              }
              void my_print (char *string)
              {
              printf (The string is %s , string);
              }
              void my_print2 (char *string)
              {
              char *string2;
              int size, i;
              size = strlen (string);
              string2 = (char *) malloc (size + 1);
              for (i = 0; i size; i++)
              string2[size -1 - i] = string;
              string2[size] = '';
              printf (The string printed backward is %s , string2);
              }
              如果程序產(chǎn)生了core文件,可以用gdb hello core命令來查看程序在何處出錯。如在函數(shù)my_print2()中,如果忘記了給string2分配內(nèi)存 string2 = (char *) malloc (size + 1);,很可能就會  core dump. linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)

            linux相關文章:linux教程




            評論


            相關推薦

            技術專區(qū)

            關閉