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

时间:2019-02-07 16:26:31

标签: c windows lzw

我进行了一些研究,但与我的问题无关...

我实际上是在为学校编写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 = '\0', 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] = '\0';

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

}

另一个谢谢你们!

1 个答案:

答案 0 :(得分:0)

我再次向我的老师展示了该程序,他发现了问题! 问题是我从不使用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;
}

首先我要对不起,因为这样的错误少,也非常感谢您的耐心!