#### Archived

This topic is now archived and is closed to further replies.

# vector does funny stuff (just to piss me off)

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

## Recommended Posts

ok, so heres my problem, im writing a Tile Based SDL thing, so my way of doing it is load in the texture file, onto one surface, then create an array of SDL_Rects to hold the location of each of the tiles. I decided to use the Vector STL so i dont have to use big arrays. so heres my code: //header stuff vector SampleTiles; /*edit: gamedev removes the angle brackets, and all the contained*/ //body stuff for (int r = 0; r < rows; r++) { for (int c = 0; c < columns; c++) { counter++; rect.x = c * tilewidth; rect.y = r * tileheight; rect.w = tilewidth; rect.h = tileheight; SampleTiles.push_back(rect); } } so anyway, when i output rect.x inside the loop, i get normal values, incrementing by 16 (the tile width), but when i ask for SampleTiles[counter].x i get these crazy numbers like -21216 so anyway, where am i going wrong? thanks a lot -- Jake [edited by - shade13 on June 7, 2004 7:35:00 PM]

##### Share on other sites
When that loop ends, counter = rows*columns.
The last element is (rows*columns)-1. (You''re overstepping your array bounds)

##### Share on other sites
My vector has no preset size, im using the SampleTiles.push_back(rect), counter is really only there for the output:

fprintf(crap, "+++ Rect #%i +++\n"
"+x: %i\n"
"+y: %i\n"
"+w: %i\n"
"+h: %i\n"
"++++++++++++++++\n\n", counter, SampleTiles[counter].x, SampleTiles[counter].y, SampleTiles[counter].w, SampleTiles[counter].h);

##### Share on other sites
can you post your code again in [*source][/source] brackets please (without the asterix)? i'd like to see how you define your vector (the part with the angle brackets. Also, please show the class definition for whatever class rect is.

here's the rest easier to read:
//EDIT: this is the line i want to see.//header stuffvector SampleTiles; //body stufffor (int r = 0; r < rows; r++) {    for (int c = 0; c < columns; c++) {	counter++;	rect.x = c * tilewidth;	rect.y = r * tileheight;	rect.w = tilewidth;	rect.h = tileheight;		        SampleTiles.push_back(rect);    }}

-me

[edited by - Palidine on June 7, 2004 7:48:34 PM]

##### Share on other sites
ok, so heres the code:

class cMap {public:	int LoadTextures(char * texturefile, int rows, int columns);		void debugOn()  { debug = 1; }	void debugOff() { debug = 0; } private:	vector <SDL_Rect> SampleTiles;	int debug;};int cMap::LoadTextures(char *texturefile, int rows, int columns) {	SDL_Rect rect;	SDL_Surface* s;	int tilewidth, tileheight, counter;	FILE * crap;	//SampleTiles.resize(rows * columns + 1);	if (debug) {		crap = fopen("cMap.txt", "w+");	}		if ((s = IMG_Load(texturefile)) == NULL) {		if (debug) fprintf(crap, "--------------------+  Couldn''t load texture file: %s\n", texturefile);		return 1;	}	tilewidth = s->w / columns;	tileheight = s->h / rows;	counter = 0;	for (int r = 0; r < rows; r++) {		for (int c = 0; c < columns; c++) {			counter++;			rect.x = c * tilewidth;			rect.y = r * tileheight;			rect.w = tilewidth;			rect.h = tileheight;			SampleTiles.push_back(rect);			fprintf(crap, "+++ Rect #%i +++\n"						  "+x:  %i\n"						  "+y:  %i\n"						  "+w:  %i\n"						  "+h:  %i\n"						  "++++++++++++++++\n\n", counter, SampleTiles[counter].x, SampleTiles[counter].y, SampleTiles[counter].w, SampleTiles[counter].h);		}	}	return 0;}

ok, i gave a bit more detail there, thanks in advance

-- Jake

##### Share on other sites
oh, rite. pretty simple.

the way you are incrementing counter you are always accessing one position beyond where you actually are in the vector.

i.e.:

vector<int> foo;int counter = 0;foo.push_back(1); //this puts 1 into the 0th position in the array//so if you:cout << foo[counter+1];//you are now beyond the bounds of your vector//this is what you are doing in your program as it's written

basically just move the counter++; below the line where you actually print things out. the way it's written now the first time you print out during your iterations counter will be == 1, however you don't have a rect in vector[1] yet, only vector[0]. the funny part, i guess, is that your code is actually fine, it was your debugging code that had the error

-me

[edited by - Palidine on June 7, 2004 8:04:02 PM]

##### Share on other sites
i thought of that, but the counter is only used for viewing.
each frame is 16 px wide/tall, so when i directly output the crap to my datafile, i get it all working, but when i go thru my vector, heres a sample:

+++ Rect #1 +++
+x: 56
+y: 2
+w: 1
+h: 0
++++++++++++++++

+++ Rect #2 +++
+x: 53
+y: 3
+w: 1
+h: 0
++++++++++++++++

+++ Rect #3 +++
+x: -21216
+y: 30661
+w: 0
+h: 0
++++++++++++++++

+++ Rect #4 +++
+x: 513
+y: 5
+w: 257
+h: 8
++++++++++++++++

+++ Rect #5 +++
+x: -11824
+y: 1280
+w: 20944
+h: 1281
++++++++++++++++

+++ Rect #6 +++
+x: -11824
+y: 1280
+w: 53712
+h: 1280
++++++++++++++++

-- Jake

[edited by - shade13 on June 7, 2004 8:04:17 PM]

ps. eating dinner, back in an hour

[edited by - shade13 on June 7, 2004 8:05:25 PM]

##### Share on other sites
no, look. it's an order of operations thing:

vector<int> foo;int counter = 0;foo.push_back(1);counter++;cout << foo[counter];//this is exactly what you are doing//counter is = 1 when you print it.  however, the number 1 is//in position foo[0].  NOT foo[1].  so you are printing beyond the//bounds of your array

this is the correct way:
 vector<int> foo;int counter = 0;foo.push_back(1);cout << foo[counter];counter++;//now counter is = 0 when you print//the number 1 will now be printed to the screen

make sense? vectors, like arrays, start element numbering at zero, not one. the first element in a vector is always accesses with vectorName[0]. vectorName[1] is actually the 2nd element in the array. you are printing to the file, the wrong information. if you actually go through your vector afterwards, you will find it contains the correct information:

for ( unsigned int i = 0; i < SampleTiles.size(); ++i ){    std::cout << SampleTiles[i].x << " " << SampleTiles[i].y << " " << SampleTiles[i].w << " " << SampleTiles[i].h << \n";}

-me

[edited by - Palidine on June 7, 2004 8:07:36 PM]

[edited by - Palidine on June 7, 2004 8:10:10 PM]

[edited by - Palidine on June 7, 2004 8:14:32 PM]

##### Share on other sites
holy crap, its so simple.

ah well, thats what programming is all about

anyway, thanks a lot, i owe you one

-- Jake

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 47
• ### Forum Statistics

• Total Topics
632636
• Total Posts
3007574
• ### Who's Online (See full list)

There are no registered users currently online

×