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 8956545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:48:40+00:00 2026-06-15T14:48:40+00:00

I’ve never seen this before. I’m seg faulting when comparing two integers. EDIT: Forgot

  • 0

I’ve never seen this before. I’m seg faulting when comparing two integers.

EDIT: Forgot to include the most frustrating part: It runs fine in DDD so I cant debug this.

Here’s my gdb session backtracing the seg fault:

> Reading symbols from /home/michael/ecs60/hw3/3/huffman...done.
[New LWP 4109]

warning: Can't read pathname for load map: Input/output error.
Core was generated by `./huffman -d'.
Program terminated with signal 11, Segmentation fault.
#0  0x000000000040123a in huffnode::operator< (this=0x1e39010, hn=...)
    at huffnode.h:54
54      if(weight < hn.weight) {
(gdb) bt
#0  0x000000000040123a in huffnode::operator< (this=0x1e39010, hn=...)
    at huffnode.h:54
#1  0x00000000004021f4 in minheap<huffnode>::add (this=0x7fff15de7490, 
    n=...) at minheap.h:65
#2  0x0000000000401cb4 in decompress () at main.cpp:198
#3  0x00000000004012bb in main (argc=2, argv=0x7fff15de75f8)
    at main.cpp:41 <

Here’s the offending code:

bool huffnode::operator<(const huffnode& hn) {
    if(weight < hn.weight) {
            return true;
    } else if(weight == hn.weight) {
            return small < hn.small;
    } else {
            return false;
    }
};

Here is the function that calls the offending code:

template<class T>
void minheap<T>::add(T n) {
        if(size + 1 > capacity)
                incCapacity();

        heap[size] = n;
        int index = size;
        if(index == 0) return;
        while(heap[index] < heap[(index + 1)/2 -1] && index != 0) {
                swap(index, ((index+1)/2 - 1));
                index = ((index + 1)/2 - 1);
        }
        size++;
};

Here’s the portion of decompress that calls minheap::add:

unsigned int freq[NUM_CHARS];
for(int i = 0; i < NUM_CHARS; i++) {
        in = getNum();
        freq[i] = in;
}

for(int i = 0; i < NUM_CHARS; i++) {
        if(freq[i] > 0) {
                tree.add(huffnode((int)freq[i], (char) i));
        }
}

Thank you, everyone! The seg fault is fixed but now half my program was apparently dependant on broken code, so back to DDD.

  • 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-15T14:48:41+00:00Added an answer on June 15, 2026 at 2:48 pm

    According to your stack trace, hn is ..., which indicates that it’s probably pointing to bad memory (and given that you’re getting a seg-fault, it’s almost certain it is). Are you sure that the huffnode being passed to the operator overload is valid? Has it been initialized?

    Examining your stacktrace further, n in yourminheap<huffnode>::add function is also invalid, meaning that add is being passed an invalid huffnode. add looks like it being called from decompress, and since decompress does not take any arguments, it’s likely in there where you are passing an invalid huffnode to add.

    Given that add is being passed directly the return value of the huffnode constructor, it looks like it won’t get an invalid object, so maybe the ... in the stack trace is misleading.

    The next problem I’d examine is your comparison heap[index] < heap[(index + 1)/1 - 1]. Note that if index is 0, then the second operator will be heap[-1], which is invalid. Can you ensure that index will never be 0 or less?

    As a side note, I’d like to point out that the entire conditional:

    heap[index] < heap[(index + 1)/2 - 1] && index != 0
    

    will fail even when index is equal to 0 because the < operator will be evaluated first, causing a seg-fault. You should either take the 0 check outside of and before the conditional, or as the first condition.

    Lastly, if this is the type of bug that only manifests itself outside of a debugger, then print statements become your friend (crude, yes, but they get the job done, if you know what to print). I’d suggest adding prints to add at each iteration of your while loop, checking what index is. You could also print in the beginning of add what the address of n is, and see if it looks like uninitialized memory. Given how you’re adding huffnodes to your data structure, I’m pretty sure you’re problem is out-of-bounds access in your heap array.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was
I know there's a lot of other questions out there that deal with this

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.