2012-09-10 169 views
0

我創建了以下代碼,一個簡單的可打印ascii強力程序。用戶傳入開始密碼長度和結束密碼長度的參數。當我的程序運行時,我得到以下錯誤:內存分配運行時錯誤C

輸入:

~$ Enter START length & END length ex:(8 10): 2 3 

輸出:

... (more output above) 
~| 
~} 
~~ 

*** glibc detected *** ./wordgen: double free or corruption (out): 0x0916e008 *** 
======= Backtrace: ========= 
/lib/i386-linux-gnu/libc.so.6(+0x6cbe1)[0x874be1] 
/lib/i386-linux-gnu/libc.so.6(+0x6e50b)[0x87650b] 
/lib/i386-linux-gnu/libc.so.6(cfree+0x6d)[0x87969d] 
./wordgen[0x80486f4] 
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x81ee37] 
./wordgen[0x8048471] 
======= Memory map: ======== 
00110000-0012a000 r-xp 00000000 08:06 3408733 /lib/i386-linux-gnu/libgcc_s.so.1Aborted 

Valgrind的輸出:

==11050== Invalid read of size 1 
==11050== at 0x804866F: main (wordgen.c:37) 
==11050== Address 0x41a2027 is 1 bytes before a block of size 3 alloc'd 
==11050== at 0x4026864: malloc (vg_replace_malloc.c:236) 
==11050== by 0x8048600: main (wordgen.c:28) 
==11050== 
==11050== Invalid write of size 1 
==11050== at 0x8048675: main (wordgen.c:37) 
==11050== Address 0x41a2027 is 1 bytes before a block of size 3 alloc'd 
==11050== at 0x4026864: malloc (vg_replace_malloc.c:236) 
==11050== by 0x8048600: main (wordgen.c:28) 
==11050== 
==11050== Invalid read of size 1 
==11050== at 0x8048689: main (wordgen.c:38) 
==11050== Address 0x41a2027 is 1 bytes before a block of size 3 alloc'd 
==11050== at 0x4026864: malloc (vg_replace_malloc.c:236) 
==11050== by 0x8048600: main (wordgen.c:28) 
==11050== 

C代碼:

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

int main(int argc, char *argv[]) { 
    int sLen = 1; 
    int eLen = 0; 

    printf("Enter START length & END length ex:(8 10): "); 
    scanf("%d %d", &sLen, &eLen); 
    int cLen = sLen; 
    while (cLen <= eLen) { 

    /* Allocate Memory for String & Initialize */ 
    char *outStr = malloc(cLen + 1); 
    memset(outStr, ' ', cLen); 
    outStr[cLen] = 0; 

    int outerControl = 1; 
    while (outerControl == 1) { 
     int cMod = 1; 
     int innerControl = 1; 
     while(innerControl == 1) { 
     outStr[cLen-cMod] += 1; 
     if((int)outStr[cLen-cMod] == 127) { 
      //Exit Condition Where The Error Occurred 
      if(cLen - cMod == 0) { outerControl = 0; } 
      outStr[cLen-cMod] = 32; 
      cMod += 1; 
     } 
     else { innerControl = 0; } 
     } 
     printf("%s\n",outStr); 
    } 
    free(outStr); // Possible source of Error? 
    cLen += 1; 
    } 

    return 0; 
} 

我是新來的C語言編程和我完全被這個錯誤感到困惑。這是什麼意思?我如何錯誤地創建我的程序?我假設它是與內存管理...

+1

嘗試使用'-Wall -g'參數進行編譯並使用'valgrind'。 – Yamaneko

+0

我沒有使用valgrind,它抑制了運行時錯誤。當我沒有valgrind運行時發生錯誤。 –

回答

3

您的問題是:

while(innerControl == 1) { 
    printf("%d %d\n", cLen, cMod); 
    outStr[cLen-cMod] += 1;      // <-- this here 
    if((int)outStr[cLen-cMod] == 127) { 
    //Exit Condition Where The Error Occurred 
    if(cLen - cMod == 0) { outerControl = 0; } 
    outStr[cLen-cMod] = 32; 
    cMod += 1; 
    } 
    else { innerControl = 0; } 
} 

在某些時候,cMod增長比cLen大,所以你訪問outStr出其邊界的(即:outStr[-1])。這種行爲是未定義的。

這種情況:

if(cLen - cMod == 0) { outerControl = 0; } 

...好像是有防止,但只會如果(int)outStr[cLen-cMod] == 127執行。您可能應該添加如下內容:

if (cMod > cLen) 
    break; 

outStr[cLen-cMod] += 1; 
+0

我從字面上理解了這一點,回來創建答案,並且已經發布了它;)將條件更改爲'if(cLen - cMod == 0){outerControl = 0; innerControl = 0; }' –

+0

@awashburn:你錯把我當成別人了,我沒有編輯答案。 ;-)無論如何,很高興你找到了它。 :) – netcoder

0

你有你的while (cLen <= eLen)每個循環過程免費outStr,所以它會正常工作僅當eLen - cLen < 1)

更新:對不起,我錯了 - 你在每次傳球時都會玩。

我試過你的程序,它在我的Mac上沒有任何錯誤。嘗試使用valgrind來找出發生的事情。