-2
void printTop5()
{
    int counter = 1;
    int x, y;
    float civ;
    cout << "Total no. of records available = " << pointVector.size();
    cout << "Printing top 5 exploration destinations....";
    sort(pointVector.begin(), pointVector.end(), ascendingCiv);
    for (int i = 0; i < 5; i++)
    {
        PointTwoD p1 = pointVector[i];
        x = p1.getX();
        y = p1.getY();
        civ = p1.getCivIndex();
        cout << counter << ")\t";
        if (civ > 0)
        {
            cout << "Civ idx: " << civ << " ; at sector(" << x << "," << y < ")\n";
        }
        else
            cout << "<No records available>";
    }
    }

bool ascendingCiv(const PointTwoD &d1, const PointTwoD &d2)
{
    return d1.getCivIndex() > d2.getCivIndex();
}

When I run this function I get this fault whereby it says segmentation fault core dumped. Any idea? Heard its about some memory problem.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 4
    Please **[edit]** your question with a [mcve] or [SSCCE (Short, Self Contained, Correct Example)](http://sscce.org) – NathanOliver Apr 20 '16 at 13:49
  • 1
    When it core dumps you can check which line of code failed, SO is not your personal debugger – Slava Apr 20 '16 at 13:50
  • bro how do i check which line of code failed i am using ubuntu – Raphael Seize Apr 20 '16 at 13:51
  • 1
    @RaphaelSeize By using a debugger of your choice. [GDB](http://manpages.ubuntu.com/manpages/trusty/man1/gdb.1.html) for example. – Algirdas Preidžius Apr 20 '16 at 13:52
  • i tried using GDB but i dont know how to run it with the prog, should i run it like this? gdb prog.exe core ? cause it doesnt run – Raphael Seize Apr 20 '16 at 13:54
  • 1
    @RaphaelSeize `for (int i = 0; i < 5; i++)` How do you know there are at least 5 items? Why not make sure by looping to the `min(5, pointerVector.size());`? – PaulMcKenzie Apr 20 '16 at 13:56
  • @RaphaelSeize you can execute your app under debugger, then when it crushes debugger will point to the problem line automatically `gdb executable` then `run` or you can load core file into debuger `gdb executable corefile` then `backtrace` – Slava Apr 20 '16 at 13:57
  • -PaulMcKenzie , im supposed to print the top 5, so if there is less than 5 it will display no records avail from the if else statement. – Raphael Seize Apr 20 '16 at 13:59
  • 1
    @RaphaelSeize No, the code that you're showing does not do that. You loop 5 times, regardless. You hard-coded `5` into the loop condition. As soon as you do this with a vector with less than 5 elements: `PointTwoD p1 = pointVector[i];` you're dead. – PaulMcKenzie Apr 20 '16 at 14:00
  • after debugging i got this Program received signal SIGSEGV, Segmentation fault. 0x0000000000402278 in PointTwoD::PointTwoD(PointTwoD const&) () – Raphael Seize Apr 20 '16 at 14:02
  • Right, and that can be caused by copying a bogus `PointTwoD` to another one, which is what that line I pointed out would attempt to do if you have less than 5 elements. Why not just fix that **big** bug in your code and retest? – PaulMcKenzie Apr 20 '16 at 14:04
  • so PointTwoD p1 = pointVector[i] , if lets say i = 3 and it doesn't exist, an error will occur? Let me give it a try and ill get back to you, thanks so much – Raphael Seize Apr 20 '16 at 14:05
  • If `3` is out of bounds of the vector, then it is an error. Better yet, don't fix the bug -- use `PointTwoD p1 = pointVector.at(i);`. In other words, use `at( )` instead of `[ ]` -- what error do you get? If you are beyond the bounds of the vector, you would get a `out_of_range` exception being thrown instead of a hard-to-diagnose SIGSEGV. – PaulMcKenzie Apr 20 '16 at 14:07
  • hey you are right i got this, terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check Total no. of records available = 0Printing top 5 exploration destinations....Aborted (core dumped) – Raphael Seize Apr 20 '16 at 14:10
  • So your vector didn't have 5 elements. Why it doesn't have 5 elements -- that's for you to figure out. What we do know is that you were accessing the vector with an index that doesn't exist. – PaulMcKenzie Apr 20 '16 at 14:16
  • Thanks I solved the problem! – Raphael Seize Apr 20 '16 at 14:26
  • @RaphaelSeize If the answer provided would have solved the issue, please accept. – PaulMcKenzie Apr 20 '16 at 21:02
  • OP: Please see [this question/answer](http://stackoverflow.com/q/33047452/472647) for more information about segfaults and debugging them. – CodeMouse92 Apr 21 '16 at 20:08

1 Answers1

0

One issue is that you are not checking whether you actually have at least 5 elements in your vector:

for (int i = 0; i < 5; i++)
{
    PointTwoD p1 = pointVector[i];

If i is out-of-bounds of the vector, accessing pointVector[i] would invoke undefined behavior.

The loop condition can be changed to the following:

#include <algorithm>
//...
size_t loopEnd = std::min(pointVector.size(), 5);
for (size_t i = 0; i < loopEnd; i++)
{
    PointTwoD p1 = pointVector[i];

The loop will either go up to the size of the vector or 5 elements, whichever is smaller.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45