0

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;
}
Joseph D.
  • 11,804
  • 3
  • 34
  • 67
Will
  • 1
  • 1
  • 2
    Have you used a debugger? – Rivasa May 09 '18 at 01:00
  • 1
    Regarding the question title: Would you be posting a question here it there wasn't? 0xcdcdcdcd is Visual Studio-ese for uninitialized heap allocation if I'm remembering correctly. Sounds like you allocated something that contained a pointer and never set the pointer. – user4581301 May 09 '18 at 01:00
  • I'm remembering correctly: https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations – user4581301 May 09 '18 at 01:01
  • What value does `p` have? that would be my first guess as to the cause. – John Perry May 09 '18 at 01:02
  • `malloc` is dangerous to use in C++. It's fine for a `char`, but `malloc` just gives the program access to memory. It does not run constructors, so if you`malloc`ed `pHC` you could easily wind up in this situation. – user4581301 May 09 '18 at 01:02
  • 2
    An [mcve] would be very helpful for untangling this, and odds are very good that while you are reducing your way to an MCVE you'll carve away everything that's preventing you from seeing and fixing the bug. Everybody wins with a good MCVE. – user4581301 May 09 '18 at 01:07
  • @JohnPerry there are a couple places where `p` is altered `p = pHT[p].lchild;` is one. – user4581301 May 09 '18 at 01:12
  • @user4581301 Thanks, don't know how I missed that; I even did a search on `p` :-) – John Perry May 09 '18 at 01:13
  • @user4581301 In that case I'd go back to the first guess: what value does `p` have? Somewhere it's changing to an undesirable value. – John Perry May 09 '18 at 01:14
  • @Annabelle Yes,I debuged it on Microsoft Visual Studio 2010. – Will May 09 '18 at 01:18
  • 2
    Will, gather information from the call stack and the variables around the crash site. If that doesn't set off a D'Oh! moment and solve the problem for you, post the information gathered. @JohnPerry 's right that we definitely want to see `p`. – user4581301 May 09 '18 at 01:21
  • @user4581301When the loop is first time,the debugger give a error.And point the exception position is: pHC[p] = (char*)malloc((cdlen + 1) * sizeof(char)). – Will May 09 '18 at 01:22
  • OK.Thank you.I will try the other method to generate huffman code. – Will May 09 '18 at 01:25
  • 1
    @Will *I tried many methods to solve the annoying problem* -- One thing you should do is things like this -- `pHC[p] = (char*)malloc((cdlen + 1) * sizeof(char)); cd[cdlen] = '\0';` -- Stop this and simply use `std::string`. Since you're using C++, you should know when to drop using `C` patterns like this and simply remove it and use `std::string`. – PaulMcKenzie May 09 '18 at 01:39
  • 1
    When the debugger stops you should be able to select the Locals tab down at the bottom and see the values of `p` `pHC` and `cdlen`. One of those should show you where you really have the problem, – user4581301 May 09 '18 at 01:40

0 Answers0