2013-06-01 60 views
-1

我在使用opendir在我的程序中打開一個目錄時遇到問題。爲什麼opendir獲取SIGABRT錯誤

我用這行做到這一點:

int main(int argc, char** argv) 
{ 
    int i; 
    int Port,TPS,QS,Height,Number_kids; 
    char server_ip[16]; 
    DIR * directory; 
    struct ps *parms = (struct ps*)malloc(sizeof(struct ps)); 

    if(argc!=8) 
    { 
     printf("Wrong number of arguents given!!!\n"); 
     return -1; 
    } 

    Port=atoi(argv[2]); 
    TPS=atoi(argv[3]); 
    QS=atoi(argv[4]); 
    Height=atoi(argv[5]); 
    Number_kids=atoi(argv[6]); 

    strcpy(server_ip,argv[1]); 
    strcpy(parms->directory,argv[7]); 


    if(Height<2 || Number_kids<2) 
    { 
     printf("Wrong parameters given!!!\n"); 
     return -1; 
    } 

    for(i=2;i<7;i++) 
    { 
     int temp=atoi(argv[i]); 
     if(temp<=0) 
     { 
      printf("Wrong parameters given!!!\n"); 
      return -1; 
     } 
    } 

    directory = opendir(parms->directory); 
    if(directory == NULL) 
    { 
     perror("Error when trying to open specified directory:"); 
     return; 
    } 
} 

其中parms->directory

struct ps 
{ 
    char directory[50]; 
}; 

目錄的功能包含的目錄路徑:/ home/user中/桌面/文件夾 (路徑是正確的)

當我運行我的程序沒有調試器我得到這個錯誤:

malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 
2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) 
(old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof 
(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) 
old_end & pagemask) == 0)' failed. 

我可以從那個身影出了差錯功能執行opendir內部時,它使用的malloc(?)

Whern我跑我得到一個SIGABRT信號和同樣的錯誤調試器。 (我正在使用Netbeans)

我無法弄清楚我在做什麼錯。任何幫助表示讚賞。

回答

2

如果malloc()以這種方式失敗,它幾乎肯定是你的程序中的一個錯誤,而且這個錯誤幾乎肯定是在其他地方。這個錯誤可能在一段完全不相關的代碼中。

這些錯誤通常是由內存損壞引起的。使用Valgrind運行程序或使用Mudflap啓用編譯。這些都必須安裝,但如果您使用C語言編寫代碼,則它們是工具箱的重要組成部分。它們更有可能指出程序中實際導致內存損壞的部分。

+0

我用valgrind,它顯示我的代碼的其餘部分沒有錯。我似乎在opendir函數中有一堆堆損壞。我沒有在那個程序中做一些非常複雜的事情。它只是一個主要的功能,只會試圖打開。 – Blenikos

+0

如果它真的只是一個'main()'函數,請發佈整個程序。您可以發佈到Gist(https://gist.github.com)或將整個程序粘貼到您的問題中,具體時間取決於它的時間長短。 –

+0

好的。我用我所有的代碼編輯了這個問題 – Blenikos