<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 練習4:Valgrind 介紹 > 原文:[Exercise 4: Introducing Valgrind](http://c.learncodethehardway.org/book/ex4.html) > 譯者:[飛龍](https://github.com/wizardforcel) 現在是介紹另一個工具的時間了,在你學習C的過程中,你會時時刻刻用到它,它就是 `Valgrind`。我現在就向你介紹 `Valgrind`,是因為從現在開始你將會在“如何使它崩潰”一節中用到它。`Valgrind`是一個運行你的程序的程序,并且隨后會報告所有你犯下的可怕錯誤。它是一款相當棒的自由軟件,我在編寫C代碼時一直使用它。 回憶一下在上一章中,我讓你移除`printf`的一個參數,來使你的代碼崩潰。它打印出了一些奇怪的結果,但我并沒有告訴你為什么它會這樣打印。這個練習中我們要使用`Valgrind`來搞清楚為什么。 > 注 > 這本書的前幾章講解了一小段代碼,同時摻雜了一些必要的工具,它們在本書的剩余章節會用到。這樣做的原因是,閱讀這本書的大多數人都不熟悉編譯語言,也必然不熟悉自動化的輔助工具。通過先讓你懂得如何使用`make`和`Valgrind`,我可以在后面使用它們更快地教你C語言,以及幫助你盡早找出所有的bug。 > 這一章之后我就不再介紹更多的工具了,每章的內容大部分是代碼,以及少量的語法。然而,我也會提及少量工具,我們可以用它來真正了解發生了什么,以及更好地了解常見的錯誤和問題。 ## 安裝 Valgrind 你可以用OS上的包管理器來安裝`Valgrind`,但是我想讓你學習如何從源碼安裝程序。這涉及到下面幾個步驟: + 下載源碼的歸檔文件來獲得源碼 + 解壓歸檔文件,將文件提取到你的電腦上 + 運行`./configure`來建立構建所需的配置 + 運行`make`來構建源碼,就像之前所做的那樣 + 運行`sudo make install`來將它安裝到你的電腦 下面是執行以上步驟的腳本,我想讓你復制它: ```sh # 1) Download it (use wget if you don't have curl) curl -O http://valgrind.org/downloads/valgrind-3.6.1.tar.bz2 # use md5sum to make sure it matches the one on the site md5sum valgrind-3.6.1.tar.bz2 # 2) Unpack it. tar -xjvf valgrind-3.6.1.tar.bz2 # cd into the newly created directory cd valgrind-3.6.1 # 3) configure it ./configure # 4) make it make # 5) install it (need root) sudo make install ``` 按照這份腳本,但是如果 `Valgrind` 有新的版本請更新它。如果它不能正常執行,也請試著深入研究原因。 ## 使用 Valgrind 使用 `Valgrind` 十分簡單,只要執行`valgrind theprogram`,它就會運行你的程序,隨后打印出你的程序運行時出現的所有錯誤。在這個練習中,我們會崩潰在一個錯誤輸出上,然后會修復它。 首先,這里有一個`ex3.c`的故意出錯的版本,叫做`ex4.c`。出于練習目的,將它再次輸入到文件中: ```c #include <stdio.h> /* Warning: This program is wrong on purpose. */ int main() { int age = 10; int height; printf("I am %d years old.\n"); printf("I am %d inches tall.\n", height); return 0; } ``` 你會發現,除了兩個經典的錯誤外,其余部分都相同: + 沒有初始化`height`變量 + 沒有將`age`變量傳入第一個`printf`函數 ## 你會看到什么 現在我們像通常一樣構建它,但是不要直接運行,而是使用`Valgrind`來運行它(見源碼:"使用Valgrind構建并運行 ex4.c"): ```sh $ make ex4 cc -Wall -g ex4.c -o ex4 ex4.c: In function 'main': ex4.c:10: warning: too few arguments for format ex4.c:7: warning: unused variable 'age' ex4.c:11: warning: 'height' is used uninitialized in this function $ valgrind ./ex4 ==3082== Memcheck, a memory error detector ==3082== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==3082== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info ==3082== Command: ./ex4 ==3082== I am -16775432 years old. ==3082== Use of uninitialised value of size 8 ==3082== at 0x4E730EB: _itoa_word (_itoa.c:195) ==3082== by 0x4E743D8: vfprintf (vfprintf.c:1613) ==3082== by 0x4E7E6F9: printf (printf.c:35) ==3082== by 0x40052B: main (ex4.c:11) ==3082== ==3082== Conditional jump or move depends on uninitialised value(s) ==3082== at 0x4E730F5: _itoa_word (_itoa.c:195) ==3082== by 0x4E743D8: vfprintf (vfprintf.c:1613) ==3082== by 0x4E7E6F9: printf (printf.c:35) ==3082== by 0x40052B: main (ex4.c:11) ==3082== ==3082== Conditional jump or move depends on uninitialised value(s) ==3082== at 0x4E7633B: vfprintf (vfprintf.c:1613) ==3082== by 0x4E7E6F9: printf (printf.c:35) ==3082== by 0x40052B: main (ex4.c:11) ==3082== ==3082== Conditional jump or move depends on uninitialised value(s) ==3082== at 0x4E744C6: vfprintf (vfprintf.c:1613) ==3082== by 0x4E7E6F9: printf (printf.c:35) ==3082== by 0x40052B: main (ex4.c:11) ==3082== I am 0 inches tall. ==3082== ==3082== HEAP SUMMARY: ==3082== in use at exit: 0 bytes in 0 blocks ==3082== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==3082== ==3082== All heap blocks were freed -- no leaks are possible ==3082== ==3082== For counts of detected and suppressed errors, rerun with: -v ==3082== Use --track-origins=yes to see where uninitialised values come from ==3082== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 4 from 4) $ ``` > 注 > 如果你運行了`Valgrind`,它顯示一些類似于`by 0x4052112: (below main) (libc-start.c:226)`的東西,而不是`main.c`中的行號,你需要使用`valgrind --track-origins=yes ./ex4`命令來運行你的`Valgrind`。由于某些原因,`valgrind`的Debian和Ubuntu上的版本會這樣,但是其它的不會。 上面那段輸出非常長,因為`Valgrind`在明確地告訴你程序中的每個錯誤都在哪兒。讓我們從開頭逐行分析一下(行號在左邊,你可以參照): 1 你執行了通常的`make ex4`來構建它。確保你看到的`cc`命令和它一樣,并且帶有`-g`選項,否則`Valgrind`的輸出不會帶上行號。 2~6 要注意編譯器也會向你報告源碼的錯誤,它警告你“向格式化函數傳入了過少的變量”,因為你忘記包含`age`變量。 7 然后使用`valgrind ./ex4`來運行程序。 8 之后`Valgrind`變得十分奇怪,并向你報錯:   14~18   在`main (ex4.c:11)`(意思是文件`ex4.c`的`main`函數的第11行)的那行中,有“大小為8的未初始化的值”。你通過查看錯誤找到了它,并且在它下面看到了“棧蹤跡”。最開始看到的那行`(ex4.c:11)`在最下面,如果你不明白哪里出錯了,你可以向上看,比如`printf.c:35`。通常最下面的一行最重要(這個例子中是第18行)。   20~24   下一個錯誤位于 `main` 函數中的 `ex4.c:11`。`Valgrind`不喜歡這一行,它說的是一些 if 語句或者 while 循環基于一個未初始化的值,在這個例子中是`height`。   25~35   剩下的錯誤都大同小異,因為這個值還在繼續使用。 37~46 最后程序退出了,`Valgrind`顯示出一份摘要,告訴你程序有多爛。 這段信息讀起來會相當多,下面是你的處理方法: + 無論什么時候你運行C程序并且使它工作,都應該使用`Valgrind`重新運行它來檢查。 + 對于得到的每個錯誤,找到“源碼:行數”提示的位置,然后修復它。你可以上網搜索錯誤信息,來弄清楚它的意思。 + 一旦你的程序在`Valgrind`下不出現任何錯誤信息,應該就好了。你可能學會了如何編寫代碼的一些技巧。 在這個練習中我并不期待你馬上完全掌握`Valgrind`,但是你應該安裝并且學會如何快速使用它,以便我們將它用于后面的練習。 ## 附加題 + 按照上面的指導,使用`Valgrind`和編譯器修復這個程序。 + 在互聯網上查詢`Valgrind`相關的資料。 + 下載另一個程序并手動構建它。嘗試一些你已經使用,但從來沒有手動構建的程序。 + 看看`Valgrind`的源碼是如何在目錄下組織的,并且閱讀它的Makefile文件。不要擔心,這對我來說沒有任何意義。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看