why am i getting a segmentation fault in my output?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anhpnt
    New Member
    • Dec 2009
    • 5

    #16
    I have run your code successfully, and the output is different from yours.
    I guess the error is somewhere outside this function.
    Also, could you send me you input file?
    You should use debugging tools to find the error more easily.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #17
      I see what you're doing now.
      You're trying to do too much with one variable.

      You need one variable to keep track of how many times you have iterated through the array (how many times you've looped).

      You should be using another variable to print the number of characters found in the array at that iteration.

      If there is 0 for a specific character then don't do anything ...just loop to the next index.

      This is a much cleaner approach that I hope will fix your problem.
      Code:
      int* getFrequency(char* filename, int arrayOfInts[])
      {  
          ifstream in;
          for(int i = 0; i < 256; i++)
          {
              arrayOfInts[i] = 0;
          }  
      
          in.open(filename);
          int c = in.get();    
           while(c!=EOF)
          {
              if(c>0 && c<256)
              {
                  arrayOfInts[c]++;
              }
              c = in.get();
          }
          in.close();
      
          cout<<"\nThe character frequencies are: \n\n";
          int numberOfCharacters = 0;
          for(int i = 0; i<256; i++)
          {   numberOfCharacters = arrayOfInts[i];
              if(i==10}
              {   cout << "\\n ";}
              if(numberOfCharacters > 0)
              {
                      cout<<char(i)<< " "<<numberOfCharacters<<"\n";
              }
          }
       
          return arrayOfInts;  
      }
      -Frinny

      PS. Could you please post code snippets in code tags.

      Comment

      • nar0122
        New Member
        • Nov 2009
        • 11

        #18
        I did not get the segmentation fault after modifying my program; however, I got some pretty crazy errors. I think it is saying I have a memory leak somewhere.

        Does this mean anything to anyone?

        *** glibc detected *** ./huffman: free(): invalid pointer: 0x00007fffd5592 6f8 ***

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #19
          Nar0122, does that mean your original problem is fixed?
          If so, please start a new thread for your new problem and indicate which post helped you to solve the problem you needed help with in this thread.

          -Frinny

          Comment

          Working...