2014-11-24 102 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