Sign in to follow this  
charlando

Function causing MAV

Recommended Posts

I can't really see why but this function seems to cause an error after compiling fine. I've searched for a solution, but not found one. The error Message is: "An Access Violation (Segmentation Fault) raised in your program." The Function:
void Map::LoadTileSheet(std::string filename, int MaxClipsX, int MaxClipsY){
     int clip = 0;
     int row;
     int column;
     for(row = 0; row = MaxClipsY; row++)
     {
             for(column = 0; column = MaxClipsX; column++)
             {
                        //Clips is a vector of SDL_Rect
                        Clips[clip].x = TilesWidth*column;
                        Clips[clip].y = TilesHeight*row;
                        Clips[clip].h = TilesHeight;
                        Clips[clip].w = TilesWidth;
                        clip++;
             }
     }
     Tileset = load_image( filename );
}

I was wondering if anyone would be able to spot a problem that i couldn't. Thanks.

Share this post


Link to post
Share on other sites
The problem is almost certainly that you are accessing the Clips vector with an out-of-bounds index. Did you try checking the value of the 'clip' index against the size of the Clips vector? I'm guessing that somewhere else you're adding MaxClipsX*MaxClipsY elements to the Clips vector; you might want to double-check that bit of the code to make sure you're actually giving yourself as much room as you seem to be assuming here. (Also, maybe make sure that you allocate space in the vector _before_ calling LoadTileSheet?)

Share this post


Link to post
Share on other sites
Ok, this is a bit annoying for me. I understand that i need to declair the size of the vector, which i didn't do. What i've done is declaired the vector in the public section of the class, which is where i want it.
I did however find out that it's possible to add more elements onto the vector by using push_back. The vector, Clips, is a vector of SDL_Rect, how do i arrange this is a line so i can edit a piece of the structure. An educated guess:
Clips[clip].x.push_back = TilesWidth*column; ?

And thanks for the help so far :)

EDIT: It's fixed, i thank you all for you help anyway. =D. There is one question though that i would like yo know...
In Arrays you start at the index 0, (int thingy[5] // 0...4), in vectors does it start at 0?

[Edited by - charlando on September 6, 2006 4:17:13 PM]

Share this post


Link to post
Share on other sites
Yes, STL vectors also use 0 as the starting index.

To add elements to a vector, you can either call push_back() to add single items, or call resize() to set a specific size for the vector:


vector<int> myVector;

// Set the vector's size to ten elements
myVector.resize(10);

// Add some integers to the vector
myVector.push_back(42);
myVector.push_back(12);
myVector.push_back(17);

// At this point, the vector contains 13 elements


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this