I’ve got a bizarre error here: 882 Segmentation fault ./a.out
The segment of code:
int end=array.Length, loop=0;
cout<<end<<" about to print";
for(;loop<end;loop++){
cout<<"\nMr. loop says: ";
array.get(loop).print(fout);
}
Output of my program:
enpty initializer called for llist
enpty initializer called for entry
adding
adding
999deleting999
done deleting
123deleting123
done deleting
333deleting333
done deleting
printing
2 about to print
./g+: line 7: 882 Segmentation fault ./a.out
and vital output of:
printing
2 about to print
./g+: line 7: 882 Segmentation fault ./a.out
meaning that the error is this line:
for(;loop<end;loop++){
which has proven good values and is syntactically correct (yes I know its bad style, though).
Ideas? No one at my university seems to be able to help me with this.
here are the files:
Unless you’re doing low-level memory operations (and you’re not doing this in the posted program), a segmentation fault is a sign of memory corruption, i.e. you’ve messed up some coding. Note that the memory corruption typically occurs before the error is triggered. In extreme cases, initial memory corruption and actual segmentation fault can be hours and modules apart.
Your first step should be to run the program in valgrind or gdb to find out the details of the segmentation fault. Also, always compile with at least
gcc -Walland take note of every warning – unless you’re modifying the compiler, changes are every single warning indicates a bug.In your case, the error is almost certainly in the implementation of
llist. There is a number of problems with that implementation:entry.towardsbackandentry.towardsfrontare usually calledprev(ious) andnext– those names are shorter and easier to distinguish.holdshould probably be calledhead, in accordance with the general naming conventions of linked lists.hold == NULL.add):The second line is almost certainly wrong. You probably want to configure
nodefirst and then just sethold->towardsback = node;.