简体   繁体   English

在C程序中使用Free时出现奇怪的行为

[英]Strange behavior when using free in c program

I have some program which decompress some string which is already mentioned here: How to decompres array of char in c . 我有一些程序可以解压缩已经在这里提到的某些字符串: 如何在c中解压缩char数组 After I finished it I have problem with function free (without it, it works ok). 完成后,我遇到了免费功能的问题(没有它,它可以正常运行)。 There is some strange behaviour and the last assert fails because of : Aborted; core dumped; 有一些奇怪的行为,最后断言失败,原因如下: Aborted; core dumped; Aborted; core dumped;

when I debug this program I found that problem is in this cycle: 当我调试该程序时,我发现问题出在这个周期:

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

it prints: 它打印:

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

can someone explain me this ? 有人可以向我解释一下吗? How can Hello world from second assert discover in third one ? 你如何从第二个断言中发现世界?

whole program is here: 整个程序在这里:

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

The problem is, that you compare a null-terminated string with a not-null-terminated string. 问题是,您将以空终止的字符串与未以空终止的字符串进行比较。 In your function decompress() you need to reserve one more int and add the missing \\0 to the copied buffer. 在函数decompress()中,您需要再保留一个int并将丢失的\\ 0添加到复制的缓冲区中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM