2014-11-24 105 views
0

我有一些程序解压缩一些已在这里提到的字符串:How to decompres array of char in c。完成后我有功能问题(没有它,它工作正常)。有一些奇怪的行为,最后断言失败的原因为:Aborted; core dumped;在c程序中使用自由时的奇怪行为

当我调试这个节目,我发现,问题出在这个周期:

for (j = 0; j < max - 1; j++) { 
     vysledek[index] = src[i - pom]; 
     printf("cccc%d\n%s\n", j,vysledek); 

     printf("xxx%c", src[i - pom]); 
     index++; 
    } 

它打印:

... 
xxx#cccc19 
HHeeeeeellllllllooooooooooooooo#####################� 
xxx#cccc20 
HHeeeeeellllllllooooooooooooooo###################### 
xxx#cccc21 
HHeeeeeellllllllooooooooooooooo####################### 
xxx#cccc22 
HHeeeeeellllllllooooooooooooooo######################## 
xxx#cccc23 
HHeeeeeellllllllooooooooooooooo#########################Hello______________________________world! 
xxx#cccc24 
HHeeeeeellllllllooooooooooooooo##########################ello______________________________world! 
... 

可以有人解释我这个?来自第二个世界的Hello world如何在第三个世界中发现?

整个程序是在这里:

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

char * decompress(const char * src) { 

    int max = 0; 
    int pom = 1; 
    int maxSize = 0; 
    int index = 0; 
    int isNumber = 0; 

    int i; 
    for (i = 0; src[i] != 0; i++) { 
     max = 0; 
     isNumber = 0; 
     while (isdigit(src[i])) { 
      int digit = (src[i] - '0'); 
      max = max * 10 + digit; 
      i++; 
      isNumber = 1; 
     } 
     if (max == 0 && !isNumber) { 
      max = 1; 
     } 

     maxSize = maxSize + max; 
    } 

    char *vysledek = (char*) malloc((maxSize) * sizeof (int)); 

    for (i = 0; src[i] != 0; i++) { 
     max = 0; 
     pom = 0; 
     isNumber = 0; 
     while (isdigit(src[i])) { 
      int digit = (src[i] - '0'); 
      max = max * 10 + digit; 
      i++; 
      pom++; 
      isNumber = 1; 
     } 

     if (!isNumber) { 
      vysledek[index] = src[i]; 
      //printf("%c", src[i]); 
      index++; 
     } else { 
      i--; 
      int j; 

      if (max < 1) { 
       index--; 
      } 


      for (j = 0; j < max - 1; j++) { 
       vysledek[index] = src[i - pom]; 
       //printf("cccc%d\n%s\n", j,vysledek); 

       //printf("xxx%c", src[i - pom]); 
       index++; 
      } 
     } 
     //printf("\n%d\n", index); 

    } 

    return vysledek; 
} 

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

    char * res; 

    assert(!strcmp(
      (res = decompress("Hello world!")), 
      "Hello world!")); 
    //free(res); 

    assert(!strcmp(
      (res = decompress("Hello_30world!")), 
      "Hello______________________________world!")); 
    //free(res); 

    assert(!strcmp(
      (res = decompress("H2e6l8o15 35w5o6r-2d0!!")), 
      "HHeeeeeellllllllooooooooooooooo         wwwwwoooooor--!!")); 

    //free(res); 
    return 0; 
} 
+0

malloc'maxSize + 1'(one more)elements and set'vysledek [maxSize]'to a magic value,的0xCC。在返回之前添加一个'assert(vysledek [maxSize] == magci_value);' – harper 2014-11-24 11:30:20

回答

0

的问题是,你比较空结束的字符串与非空结尾的字符串。 在函数decompress()中,您需要保留一个int并将缺少的\ 0添加到复制的缓冲区中。

char *vysledek = (char*) malloc((maxSize) * sizeof (int) + sizeof(int)); 
[...] 
vysledek[index] = '\0'; 
+0

hm thx这修复了我的程序,但没有解释免费功能 – hudi 2014-11-24 12:11:34

+0

我无法用_free()_重现您的问题。究竟发生了什么? – LMF 2014-11-24 12:14:37