在c程序中使用free时的奇怪行为

时间:2014-11-24 11:25:36

标签: c free

我有一些程序可以解压缩一些已在此处提到过的字符串:How to decompres array of char in c。我完成它后,我有功能免费的问题(没有它,它工作正常)。有一些奇怪的行为,最后一个断言失败的原因是:Aborted; core dumped;

当我调试这个程序时,我发现问题出现在这个循环中:

    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;
}

1 个答案:

答案 0 :(得分:0)

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

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