元素在调用函数时消失 - LZW 压缩

Element disapears while calling a function - LZW Compression

我做了一些研究,但没有什么真正与我的问题有关...

我实际上正在尝试为学校编写 LZW 压缩代码,我需要一个函数来检查某个元素是否在我的字典中。

然而,当我调用这个函数时,它试图访问我的字典中的第 64 个元素,但是它已经消失了!!我在调用函数之前检查过它,它就在这里!!更糟糕的是,我可以在之前的函数调用中调用这个元素。

你能帮帮我吗?

函数:

int is_in_dictionnary(dico * p_pRoot, char * p_string){
    int i = 0, j = 0;
    char a[1024] = { 0 }, b[1024] = { 0 };

    //strcpy(b, p_pRoot->m_dico[64].m_info);


    for (i = 0; i < p_pRoot->m_index; i++){
        printf("dico %s\n", p_pRoot->m_dico[i].m_info);

        strcpy(a, p_string);
        strcpy(b, p_pRoot->m_dico[i].m_info);

        j = strcmp(a, b);

        if (j == 0)
            return i;
    }
    return -1;
}

The console, we are herer abble to see that the function previously called the 64th element "@", whithout any problem

The error on visual studio


有些人要求我在无法运行的地方添加代码部分:

void lzw_compress(dico *p_pRoot, char * path)
{
    FILE *pFile = NULL, *pCompFile = NULL;

    int len_c = 0, size_tamp = 0, i = 0, masked_tamp = 0, tamp_to_write = 0, index_tamp = 0, a;
    unsigned char char_tamp = 0, cAndTamp[1024] = { 0 }, tampon[1024] = { 0 }, c = '[=11=]', temp[2] = { 0 };

    char test[128] = { 0 };

    pFile = fopen(path, "r+");
    if (!pFile)
    {
        printf("problem while opening file to compress");
        return;
    }


    size_t len = strlen(path);      //creation of the output file name : paht+ ".lzw"
    unsigned char *compress_name = malloc(len + 4 + 1);
    strcpy(compress_name, path);
    compress_name[len] = '.';
    compress_name[len + 1] = 'l';
    compress_name[len + 2] = 'z';
    compress_name[len + 3] = 'h';
    compress_name[len + 4] = '[=11=]';

    pCompFile = fopen(compress_name, "w");  //creation of the output file

    free(compress_name);

    while (1)
    {
        if (feof(pFile))
            break;

        c = freadByte(pFile);


        for (i = 0; i < 1024; i++)
            cAndTamp[i] = 0;

        temp[0] = c;

        strcat(cAndTamp, tampon);
        strcat(cAndTamp, temp);

        strcpy(test, p_pRoot->m_dico[64].m_info);

        a = 0;


        if (is_in_dictionnary(p_pRoot, cAndTamp) > -1)
        {
            strcpy(tampon, cAndTamp);
            a = 0;
        }

        else
        {
            if (is_in_dictionnary(p_pRoot, tampon) < 256)   //write the character in the file
            {
                char_tamp = tampon[0];
                fwrite(&char_tamp, sizeof(char), 1, pCompFile);
                a = 0;
            }

            else
            {

                a = 0;

                index_tamp = is_in_dictionnary(p_pRoot, tampon);

                a = 0;

                for (i = 0; i < p_pRoot->m_size; i++)
                {
                    mask = 1 << i;
                    masked_tamp = index_tamp & mask;
                    tamp_to_write = masked_tamp >> i;

                    fwriteBit(tamp_to_write, pCompFile);
                    flush(pCompFile);

                }
            }

            strcpy(test, p_pRoot->m_dico[64].m_info);   //HERE IT'S OK

            add_dictionnary(p_pRoot, cAndTamp, size_tamp + 1);  //add the string tamp + read byte in the dictionnay

            strcpy(test, p_pRoot->m_dico[64].m_info);       //HERE IT IS NOT OK

            strcpy(tampon, temp);
        }

        strcpy(test, p_pRoot->m_dico[64].m_info);
        size_tamp = is_in_dictionnary(p_pRoot, tampon);
    }

    if (tampon < 256)   //write the character in the file
    {
        char_tamp = (char)tampon;
        fwrite(&char_tamp, sizeof(char), 1, pCompFile);
    }

    else
    {
        index_tamp = is_in_dictionnary(p_pRoot, tampon);
        for (i = 0; i < p_pRoot->m_size; i++)
        {
            mask = 1 << i;
            masked_tamp = index_tamp & mask;
            tamp_to_write = masked_tamp >> i;

            fwriteBit(tamp_to_write, pCompFile);
            flush(pCompFile);
        }
    }


    fclose(pFile);
    fclose(pCompFile);

}

我觉得哪里有问题的功能

void add_dictionnary(dico * p_pRoot, char * p_string, int p_stringSize)
{
    p_pRoot->m_index++;

    if (p_pRoot->m_index ==  pow(2, p_pRoot->m_size))
        realloc_dictionnary(p_pRoot);


    p_pRoot->m_dico[p_pRoot->m_index].m_info = (char*)calloc(p_stringSize, sizeof(char));
    strcpy(p_pRoot->m_dico[p_pRoot->m_index].m_info, p_string);

}

再次感谢大家!

我又给老师看了一遍程序,他发现了问题! 问题是我从不使用 malloc 并且很少使用 realloc 所以这就是问题所在:

void realloc_dictionnary(dico * p_pRoot)
{
    int real = p_pRoot->m_size + 1;
    int size = pow(2, real);

    printf("index %d, previous pow %d, new power %d, size %d\n", p_pRoot->m_index, p_pRoot->m_size, real, size);

    p_pRoot->m_dico = (code*) realloc(p_pRoot->m_dico, size);

    p_pRoot->m_size = real;
}

size 位的数量,... 所以更正是:size * sizeof(code)!

void realloc_dictionnary(dico * p_pRoot)
{
    int real = p_pRoot->m_size + 1;
    int size = pow(2, real);

    printf("index %d, previous pow %d, new power %d, size %d\n", p_pRoot->m_index, p_pRoot->m_size, real, size);

    p_pRoot->m_dico = (code*) realloc(p_pRoot->m_dico, size * sizeof(code));

    p_pRoot->m_size = real;
}

首先对这个小错误表示抱歉,同时非常感谢您的耐心等待!