2012-09-06 36 views
0

我是C的新手,學習使用Zed Shaw的Learn C the Hard Way。我在查找爲什麼出現以下內存錯誤時遇到了一些問題。我敢肯定,自從我使用Valgrind檢查我的代碼以來,我沒有泄漏內存。C中的內存錯誤

這裏是我的代碼有問題的行:

: int main(int argc,char *argv[]){ 

     if (argc<3) die ("USAGE : ex17 <dbfile> <action> [action params]"); 
     ...char *filename=argv[1];// how are we able to get away with one dimension? 
     char action =argv[2][0]; 
     /*Line 166*/:struct Connection *conn=Database_open(filename,action);// error throwing line 

Database_open被定義爲函數:

/*Line 61*/:struct Connection *Database_open(const char *filename, char mode) 
{ 
    /* data */ 
    struct Connection *conn= malloc(sizeof(struct Connection)); 

    if(!conn->db) die("Memory Error");//? 

    if(mode=='c'){ 
     conn->file=fopen(filename,"w"); 
    } 
    else{ 
     conn->file=fopen(filename,"r+"); 


    if(conn->file){ 
     Database_load(conn); 
    } 
    } 


     if(!conn->file) die("Failed to open file"); 
     return conn; 
    }; 

而這裏的芯片功能,即時通訊使用:

void die (const char *message) 
{ 
     if(errno){ 
      perror(message); 
     } 
     else{ 
      printf("ERROR :%s\n", message); 
     } 
     exit(1); 
} 

當我使用Valgrind運行代碼時,我得到:

==6112== Command: ./ex17 db.dat c 
==6112== 
==6112== Conditional jump or move depends on uninitialised value(s) 
==6112== at 0x80487B3: Database_open (ex17.c:61)//Marked above 
==6112== by 0x8048BAA: main (ex17.c:166)// Line numbers marked above 
==6112== 
ERROR :Memory Error 
==6112== 
==6112== HEAP SUMMARY: 
==6112==  in use at exit: 8 bytes in 1 blocks 
==6112== total heap usage: 1 allocs, 0 frees, 8 bytes allocated 
==6112== 
==6112== LEAK SUMMARY: 
==6112== definitely lost: 0 bytes in 0 blocks 
==6112== indirectly lost: 0 bytes in 0 blocks 
==6112==  possibly lost: 0 bytes in 0 blocks 
==6112== still reachable: 8 bytes in 1 blocks 
==6112==   suppressed: 0 bytes in 0 blocks 
==6112== Rerun with --leak-check=full to see details of leaked memory 
==6112== 
==6112== For counts of detected and suppressed errors, rerun with: -v 
==6112== Use --track-origins=yes to see where uninitialised values come from 
==6112== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) 

編輯

結構連接

struct Connection 
{ 
    /* data */ 
    FILE *file; 
    struct Database *db; 
}; 
+0

'//我們如何能夠脫離一個維度?'argv是'char **'。因此,它用作數組,它包含指針,其中一個指向變量。 – chris

+0

您能否至少給出結構連接的定義,以便我們知道什麼是元素db?請記住,malloc的結構不會初始化其中的任何變量。 –

+0

@DeepanjanMazumdar:我編輯了這個問題並添加了詳細信息 – KodeSeeker

回答

1

謝謝您提供有關結構Connection的詳細信息。如所看到的,該結構包含兩個指針元素作爲成員。

分配結構後,您必須在其中專門設置malloc元素Database *db

喜歡的東西:

struct Connection *conn= malloc(sizeof(struct Connection)); 
conn->db = malloc(sizeof(struct Database)); 

希望這有助於。

+0

在' - >'令牌之前,我得到一個編譯時錯誤:error:expected')'。你可以請張貼正確的代碼,因爲Im新到C.謝謝 – KodeSeeker

+0

@KodeSeeker,我編輯了代碼..謝謝指出。經測試無誤。 –

2
struct Connection *conn = malloc(sizeof(struct Connection)); 

if(!conn->db) die("Memory Error"); // ? 

你希望conn->db是沒有建立新創建conn變量的內容非零值。也許在那個地方偶然會有一個零。請記住,使用malloc()時,不能保證返回的內存的初始內容是什麼。

+0

感謝您的回覆。那麼,如何正確設置'conn'的內容呢? – KodeSeeker