Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts

    c++ vectors - annoying bug

    I'm at a loss with this... can anyone see any errors or stupid mistakes?
    It compiles, but I get an illegal operation (causes an invalid page fault in MSVCRT.DLL) when I run the exe, in the eighth iteration of the loop, between "pushed back" and "fread..."
    I would say the fread is reading to somewhere it shouldn't but have no idea why.
    "cmesh" is a class, "cmesh->v" is a e_Vertex vector, "cmesh->v.object" is an e_Vector3f struct, new_vertex is a e_Vertex

    struct e_Vector3f{
    float x,y,z;
    };

    struct e_Vertex{
    e_Vector3f object;
    e_Vector3f world;
    e_Vector2f mapcoord;
    };


    for(i=0;i<l_qty;i++){//read vertices
    cmesh->v.push_back(new_vertex);
    dumpstr("pushed back...");
    fread(&cmesh->v[i].object.x,sizeof(float),1,l_file);
    fread(&cmesh->v[i].object.y,sizeof(float),1,l_file);
    fread(&cmesh->v[i].object.z,sizeof(float),1,l_file);
    dumpstr("fread...");


    Previously I used malloc with it working fine, but wanted to use vectors as they should be simpler.
    (I haven't used [code] tags as they might not cope with the [i] in my code )
    any ideas would be great! thanks :-)
    Last edited by _Dan; 02-23-2005 at 11:26 PM.
    Daniel
    XZIST.org

  • #2
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay..something weird: It works if I read into the new_vertex THEN push it back, but not as in the above code, where I push back a new_vertex then read into it...
    I guess I don't really need help now, but if anyone can see a simple mistake I would be interested...

    EDIT: Is there some problem with reading into a vector?
    Code:
    for(i=0;i<l_qty;i++){
    	fread(&cmesh->v[i].mapcoord.u,sizeof(float),1,l_file);
            fread(&cmesh->v[i].mapcoord.v,sizeof(float),1,l_file);
    }
    Now THIS code doesn't work. "v" is a vector....
    Last edited by _Dan; 02-23-2005 at 05:41 PM.
    Daniel
    XZIST.org

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Reading into a a vector is fine, although the normal way to do is is to read your data into your struct then push_back the struct. Either way it should't be a problem, but it's usually easier to read the code when you do it the 'normal' way.

    One thing I do see though is that unless you made a typo you aren't allocating enough space for the data.. You said "new_vertex is a e_Vector3f" but shouldn't new_vertex be a "e_Vertex" instead? You need to vector.push_back(e_Vertex) not vector.pust_back(e_Vector3f).

  • #4
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry, that was a typo new_vertex is an e_Vertex thanks *edits post*
    I really don't understand this...in my second post U and V are both definitely floats, and from the previous code working, I know cmesh->v is valid...and it crashes before any iterations of the loop. on the first fread..
    Last edited by _Dan; 02-23-2005 at 11:30 PM.
    Daniel
    XZIST.org

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you'll have to post more of your code to figure out the problem, because writing to a vector isn't a problem. How is cmesh allocated? What is the file format you're reading from? What mode was the file opened in? etc...


    Also, if you haven't done so yet, you should build a debug version and run it in the debugger
    Last edited by aman; 02-24-2005 at 12:23 AM.

  • #6
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts
    cmesh is a pointer to another MESH which is a vector (it IS okay to have a vector of a class, which in turn contains vectors..and so on?)
    File format is .3ds, opened in "rb" mode.
    a weird thing is that after the "for" in the first code, cmesh->v.size() becomes a huge number (2105656) when it should be (in my test) 286.
    But even though, my code should get up to 286 "for" iterations before crashing, but doesn't crashes on the first fread...

    As for debugging, i'm new to this...as far as I can see the problem occurs in this part of the stl_vector.h (at the last line dev-cpp throws an "access violation -segmentation fault" message)...
    Code:
          // element access
          /**
           *  @brief  Subscript access to the data contained in the %vector.
           *  @param  n  The index of the element for which data should be accessed.
           *  @return  Read/write reference to data.
           *
           *  This operator allows for easy, array-style, data access.
           *  Note that data access with this operator is unchecked and
           *  out_of_range lookups are not defined. (For checked lookups
           *  see at().)
           */
          reference
          operator[](size_type __n) { return *(begin() + __n); }
    Last edited by _Dan; 02-25-2005 at 07:48 PM.
    Daniel
    XZIST.org

  • #7
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well it's obvious that the vector is invalid.. probably the cmesh pointer that contains it that's bad. In your debugger you can step back through the stack and see what values the local/global variables have, and you could also place a watch on the cmesh pointer to follow it througout creation.

    Make sure cmesh actually is pointing somewhere valid.
    MESH *cmesh = NULL;
    cmesh = new MESH; or cmesh = &existing_mesh;
    if( cmesh != NULL) //were good

    Other than that, it sounds like you've made the program a bit complicated with vectors of classes of vectors etc.., so you'll have to go back and re-check things till you find out where you went wrong.

  • #8
    New Coder
    Join Date
    Feb 2005
    Location
    UK
    Posts
    90
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have been checking out cmesh like you said...and have found the problem

    I had to change this

    cmesh=&obj->mesh[obj->mesh.size()];

    to this...

    cmesh=&obj->mesh[obj->mesh.size()-1];

    I forgot that I need to take 1 from size() as it returns the number of elements not the actual index of the last one...
    Thanks for your help
    Daniel
    XZIST.org


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •