我以前从未见过这种情况。比较两个整数时出现段错误。
编辑:忘记包括最令人沮丧的部分:它在 DDD 中运行良好,所以我无法调试它。
这是我的 gdb 会话回溯段错误:
> 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 <
这是有问题的代码:
bool huffnode::operator<(const huffnode& hn) {
if(weight < hn.weight) {
return true;
} else if(weight == hn.weight) {
return small < hn.small;
} else {
return false;
}
};
这是调用违规代码的函数:
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++;
};
下面是调用 minheap::add 的 decompress 部分:
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));
}
}
谢谢大家!段错误已修复,但现在我的程序的一半显然依赖于损坏的代码,所以回到 DDD。