Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:07:03+00:00 2026-06-15T09:07:03+00:00

I am having a bit of trouble with my Huffman Decoding function I have

  • 0

I am having a bit of trouble with my Huffman Decoding function I have created. I was just wondering if anyone knew why my program was producing an infinite loop. Below is my function and how I caled it. When counter hits 8, it should exit out of the function because there are no more bits to read. Here it is:

HuffmanNode *rodee = createTree(freqArray2, 256); //holds the huffman tree
HuffmanNode *temporaryNode; //temporary node for traversing
temporaryNode = rodee; //first the temporary node is equal to the root
while(cin.read((char*)&w, sizeof(w))
{
  traverseCode(temporaryNode, rodee, bits, count);
  count = 0; //reset the count back to 0 for the next time the function is called 
} //as I read in a byte of 8 bits (I converted the bytes to bits in another function not shown

void traverseCode(HuffmanNode *temp, HuffmanNode *root, unsigned char *bits, int counter)
{
    if(counter >= 7)
    {
      counter = 0;
      return; 
    }
    if(temp->getLeft() == NULL && temp->getRight() == NULL)
    {
      cout << temp->getLetter();
      temp = root; 


      traverseCode(temp, root, bits, counter);
    }
    if((int)bits[counter] == 0)
    {
      traverseCode(temp->getLeft(), root,  bits, counter++);
    }
    if((int)bits[counter] == 1)
    {
      traverseCode(temp->getRight(), root, bits, counter++);
    }
}

Might anyone know why my function is going into an infinite loop and how to fix this? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:07:04+00:00Added an answer on June 15, 2026 at 9:07 am

    If you expect counter to be updated by the traverseCode() function, it needs to be a reference or a pointer. In your code it’s just a local variable, discarded when the function exits.

    So this does nothing, other than return:

    if(counter >= 7)
    {
      counter = 0;
      return; 
    }
    

    This next bit is also confusing. It will call the function with the original value of ‘counter’, which may well be the source of your infinite loop. If it did actually return, it would increment the local value of counter and then then fall through to the next if(), which is also possibly unintended.

    if((int)bits[counter] == 0)
    {
      traverseCode(temp->getLeft(), root,  bits, counter++);
    }
    if((int)bits[counter] == 1)
    {
      traverseCode(temp->getRight(), root, bits, counter++);
    }
    

    So you probably need to be handling counter completely differently, as well as not having your if() statements fall through like that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of trouble with my regex and was wondering if anyone
Having a bit of trouble using the List.Find with a custom predicate i have
I'm having a bit of trouble with a a function that the mongodb c
I'm having a bit of trouble iterating and retrieving width() values. $(.MyClass).each( function ()
Having a bit of trouble getting this right... The main objective is to have
I am having a bit of trouble with ADO. I have deployed a database
I am having a bit of trouble figuring this one out, basically I have
having a bit of trouble adding some data to a database. I have the
Having a bit of trouble here. I've created a custom ImageView by subclassing ImageView
Having a bit of trouble moving objects I've created into an array. So what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.