gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:22:05 25 4
gpt4 key购买 nike

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

我实际上正在尝试为学校编写 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);

}

再次谢谢大家!

最佳答案

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

首先我要对这个如此小的错误表示歉意,同时也非常感谢您的耐心等待!

关于c - 调用函数时元素消失 - LZW 压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54577858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com