C中的内存分配运行时错误

C中的内存分配运行时错误,c,memory-management,runtime-error,C,Memory Management,Runtime Error,我创建了以下代码,一个简单的暴力程序,用于可打印ascii。用户传入起始密码短语长度和结束密码短语长度的参数。当我的程序运行时,出现以下错误: 输入: ~$ Enter START length & END length ex:(8 10): 2 3 ... (more output above) ~| ~} ~~ *** glibc detected *** ./wordgen: double free or corruption (out): 0x0916e008 *** ===

我创建了以下代码,一个简单的暴力程序,用于可打印ascii。用户传入起始密码短语长度和结束密码短语长度的参数。当我的程序运行时,出现以下错误:

输入:

~$ 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
==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==
#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;
}
输出:

~$ 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
==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==
#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;
}
Valgrind输出:

~$ 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
==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==
#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代码:

~$ 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
==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==
#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;
}
#包括
#包括
#包括
int main(int argc,char*argv[]){
int-sLen=1;
int-eLen=0;
printf(“输入起始长度和结束长度ex:(8 10):”;
scanf(“%d%d”,&sLen,&eLen);
int-cLen=sLen;

while(cLen你在你的
的每一次传球中都有自由的出击)while(cLen你在
的每一次传球中都有自由的出击)while(cLen你的问题是:

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; }
}
…似乎是为了防止出现这种情况,但只有在
(int)outtr[cLen cMod]==127时才会执行。您可能应该添加如下内容:

if (cMod > cLen)
    break;

outStr[cLen-cMod] += 1;
你的问题是:

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; }
}
…似乎是为了防止出现这种情况,但只有在
(int)outtr[cLen cMod]==127时才会执行。您可能应该添加如下内容:

if (cMod > cLen)
    break;

outStr[cLen-cMod] += 1;

尝试使用
-Wall-g
参数编译并使用
valgrind
。我确实使用了valgrind,它抑制了运行时错误。当我在没有valgrind的情况下运行时,出现了错误。尝试使用
-Wall-g
参数编译并使用
valgrind
。我确实使用了valgrind,它抑制了运行时错误。当我在没有valgrind的情况下运行时,它抑制了运行时错误错误发生了。我真的发现了这一点,回来创建答案,你已经发布了;)将条件更改为
如果(cLen-cMod==0){outerControl=0;innerControl=0;}
@awashburn:你把我错当成了别人,我没有编辑答案;-,不管怎样,很高兴你找到了它。:)我真的刚刚弄明白了这一点,回来创建了答案,你已经发布了;)将条件更改为
如果(cLen-cMod==0){outerControl=0;innerControl=0;}
@awashburn:你把我错当成别人了,我没有编辑答案;-,不管怎样,很高兴你找到了。:)