There is a error when running the Huffman code. I tried many methods to solve the annoying problem but haven't found the solution.
Thanks a lot in advance.
This is the error:
Unhandled exception at 0x557fd51c (msvcr100d.dll) in HuffmanCompressCPro.exe: 0xC0000005: An access violation occurred while reading location 0xcdcdcdcd;
When I debug it,the position is:
pHC[p] = (char*)malloc((cdlen + 1) * sizeof(char));
CODE
int HuffmanCode(HCode &pHC,HTree &pHT)
{
char cd[256] = {'0'};
int cdlen = 0;
pHC = (char**)malloc(512 * sizeof(char*));
for(int i = 1;i < 512;i++)
{
pHT[i].weight = 0;
}
int p = 511;
while(p != 0)
{
if(pHT[p].weight == 0)
{
pHT[p].weight = 1;
if(pHT[p].lchild != 0)
{
p = pHT[p].lchild;
cd[cdlen++] = '0';
}else if(pHT[p].rchild == 0)
{
pHC[p] = (char*)malloc((cdlen + 1) * sizeof(char));
cd[cdlen] = '\0';
strcpy(pHC[p],cd);
}
}
else if(pHT[p].weight == 1)
{
pHT[p].weight = 2;
if(pHT[p].rchild != 0)
{
p = pHT[p].rchild;
cd[cdlen++] = '1';
}
}else
{
pHT[p].weight = 0;
p = pHT[p].parent;
--cdlen;
}
}
return 1;
}