Segmentation fault.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zzho039
    New Member
    • Sep 2007
    • 2

    Segmentation fault.

    hi, im doing a stage 2 compsys paper in uni.
    got stuck with the project
    could some1 have a look at my code and tell me why there's segmentation fault, plz?

    this project is simplified to find the white rectangle on the red wall of a given image.

    my main:

    [CODE=cpp] BMP bmpIn;
    bmpIn.load("tes t3.jpg");
    bmpIn.moveCurso rTo(0,30);

    firstvertex = bmpIn.findFirst White();
    one.x = firstvertex.x;
    one.y = firstvertex.y;
    cout<<one.x<<" "<<one.y<<e ndl;

    jb = bmpIn.findVerti ces2(one.x, one.y);
    lj = bmpIn.findVerti ces3(one.x, one.y);

    cout<<jb.x<<"." <<jb.y<<endl;
    cout<<lj.x<<"." <<lj.y<<endl;[/code]
    where the cursor keeps track where we are. and all the declare stuff are not here

    cpp file:

    [code=cpp]OJ BMP::findVertic es2(int a , int b)
    {
    int x, y;
    int c = 1;
    OJ jb;
    vector<int>sox;
    vector<int>soy;
    sox[0] = getCursorX();
    soy[0] = getCursorY();
    cout<<a<<" "<<b<<endl;
    cout<<"hi"<<end l;
    for (x = a; x < a + 30; x++){
    for(y = b; y< 85;y++){
    if (testwhite(x,y) ==1)
    {
    sox[c] = x;
    soy[c] = y;
    c++;
    y = b;
    x++;
    cout<<"fk"<<end l;
    }
    if (sox[c] - sox[c -1] > 10 )
    {
    jb.x = sox[c-1];
    jb.y = soy[c-1];
    return jb;
    cout<<"yuou"<<e ndl;
    }
    if (y == 84 )
    {
    jb.x = sox[c-1];
    jb.y = soy[c-1];
    cout<<"lol"<<en dl;
    }
    }}
    }

    OJ BMP::findVertic es3(int a , int b)
    {
    int x, y;
    int c = 1;
    OJ lj;
    vector<int>hix;
    vector<int>hiy;
    hix[0] = getCursorX();
    hiy[0] = getCursorY();
    for (x = a; x > a + 30; x--){
    for(y = b; y < 85;y++){
    if (testwhite(x,y) ==1)
    {
    hix[c] = x;
    hiy[c] = y;
    c++;
    y = b;
    x++;
    }
    if (hix[c] - hix[c -1] > 10 )
    {
    lj.x = hix[c-1];
    lj.y = hiy[c-1];
    return lj;
    }
    if (y == 84 )
    {
    lj.x = hix[c-1];
    lj.y = hiy[c-1];
    }
    }}

    }[/CODE]
    where OJ is a struct i created in the header file with x and y as its member
    testwhite() is basically the red green and blue values for defining white.
    Last edited by Ganon11; Oct 20 '07, 02:41 AM. Reason: Please use the [CODE] tags provided.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I am going to explain the errors in 1 function but you are making them generally through the code posted.

    [code=cpp]OJ BMP::findVertic es2(int a , int b)
    {
    int x, y;
    int c = 1;
    OJ jb;
    vector<int>sox;
    vector<int>soy;
    sox[0] = getCursorX();
    soy[0] = getCursorY();
    cout<<a<<" "<<b<<endl;
    cout<<"hi"<<end l;
    for (x = a; x < a + 30; x++){
    for(y = b; y< 85;y++){
    if (testwhite(x,y) ==1)
    {
    sox[c] = x;
    soy[c] = y;
    c++;
    y = b;
    x++;
    cout<<"fk"<<end l;
    }
    if (sox[c] - sox[c -1] > 10 )
    {
    jb.x = sox[c-1];
    jb.y = soy[c-1];
    return jb;
    cout<<"yuou"<<e ndl;
    }
    if (y == 84 )
    {
    jb.x = sox[c-1];
    jb.y = soy[c-1];
    cout<<"lol"<<en dl;
    }
    }}
    }
    }[/CODE]
    You declare 2 vectors sox and soy, you immediately access them using the index [0] and later access them using the the index [c] and [c-1]. Noting that c is initialised to 1 and increments this is not in itself a problem.

    The problem is that at no time do you ever allocate any storage to the vectors sox and soy so all these accesses are to invalid addresses and that is causing you segmentation fault. If you had used the member function <vector>.at(... ) instead of [] you would be getting out of range exceptions, the at member range checks it's input but [] is not required to do this.

    There are 2 ways to allocate memory to a vector:
    1. allocate it as you put entries in the vector using the method push_back. This function increases the array size by 1, allocating extra data if required and then inserts the value it is given in the new (final) position. This can be very inefficient if adding a lot of elements to the array because each reallocation of data is likely to require copying the current entire array from one location to another. This can be mitigated by calling reserve(...) first which allocates memory ready for an increase in size.
    2. allocate it all in one go using the method resize. This member function alters the size of the vector to any size(memory permitting) and allocates data for the vector. You can then access it using [] or .at

    Comment

    Working...