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.
According to your stack trace,
hnis..., 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 thehuffnodebeing passed to the operator overload is valid? Has it been initialized?Examining your stacktrace further,
nin yourminheap<huffnode>::addfunction is also invalid, meaning thataddis being passed an invalidhuffnode.addlooks like it being called fromdecompress, and sincedecompressdoes not take any arguments, it’s likely in there where you are passing an invalidhuffnodetoadd.Given that
addis being passed directly the return value of thehuffnodeconstructor, 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 ifindexis0, then the second operator will beheap[-1], which is invalid. Can you ensure thatindexwill never be0or less?As a side note, I’d like to point out that the entire conditional:
will fail even when
indexis equal to0because the<operator will be evaluated first, causing a seg-fault. You should either take the0check 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
printstatements become your friend (crude, yes, but they get the job done, if you know what to print). I’d suggest adding prints toaddat each iteration of yourwhileloop, checking whatindexis. You could also print in the beginning ofaddwhat the address ofnis, and see if it looks like uninitialized memory. Given how you’re addinghuffnodes to your data structure, I’m pretty sure you’re problem is out-of-bounds access in yourheaparray.