# Function causing MAV

This topic is 4184 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
The second statement in your for-loops is wrong. They should be
     for(row = 0; row < MaxClipsY; row++)     {             for(column = 0; column < MaxClipsX; column++)             {

##### Share on other sites
Thanks, i changed it but i still get the same error.

##### Share on other sites
Double check the size of your Clips[] array. It needs to be MaxClipsX*MaxClipsY large.

##### Share on other sites
Are you sure "Clips" contains at least MaxClipsX * MaxClipsY elements?

##### 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 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 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 elementsmyVector.resize(10);// Add some integers to the vectormyVector.push_back(42);myVector.push_back(12);myVector.push_back(17);// At this point, the vector contains 13 elements