# problem while using vector.push_back [FIXED]

## Recommended Posts

jake_Ghost    103
well i have made a class for heightmaps which i will use for terrain. I have all of different heightmap files in a vector. heres my struct
struct TERRAIN
{
BYTE HeightMap[1024*1024];
int MAP_SIZE;
int ID;
};


the thing is, when i push a vector with the hieghtmap data, the array is way to big for the vector (i think), so it causes an error. If i reduce the size of the byte array then it will work properly. Is there a way to fix this? thx jake [Edited by - jake_Ghost on March 28, 2005 6:36:43 PM]

##### Share on other sites
Rayno    511
What happens when you try to push that structure? Do you get an error message or something?

##### Share on other sites
Fruny    1658
You could replace the BYTE array itself with a vector<BYTE>. A 1M struct is huge; it won't fit on the stack.

##### Share on other sites
Anon Mike    1098
You realize that structure is over 1MB big? Hopefully you don't want to push to many of them.

Anyway, it helps to mention what the error is when you say you get an error.

##### Share on other sites
jake_Ghost    103
its one of those window errors that says send error report or dont send. i am using the code from gametutorials.com for raw loading.

##### Share on other sites
jake_Ghost    103
i just tried using vector<BYTE>. Had no effect. still got same error

##### Share on other sites
Rayno    511
I think the problem is probably that, even though vector uses the heap, when you do push_back you pass it a TERRAIN object that was created on the stack. And like Fruny said, that structure won't fit on the stack.

##### Share on other sites
Fruny    1658
Quote:
 Original post by jake_Ghostits one of those window errors that says send error report or dont send. i am using the code from gametutorials.com for raw loading.

It's a stack overflow. The default stack size is generally only 1MB. A single variable of that type is already bigger than that. Use dynamic allocation (e.g. a vector<BYTE>) instead of an array.

##### Share on other sites
jake_Ghost    103
ok well i cant use vector unfortunatly cause it screws up some other code i have so how do i do it normally using pointers and new statment.

can i do it like this??
BYTE *HeightMap;//and when i push the vector resize the heightmap arrayHeightMap = new BYTE[1024*1024];

##### Share on other sites
jake_Ghost    103
ok thx guys i got it working now.

##### Share on other sites
Rayno    511
Here, use something like this:

	struct TERRAIN	{		TERRAIN(){			HeightMap.resize( 1024 );			int i;			for( i = 0; i < 1024; i++ ){				HeightMap[i].resize( 1024 );			}		}		std::vector< std::vector<BYTE> > HeightMap;		int MAP_SIZE;		int ID;	};

I tested that, and it works. You should be able to use it just like your other version then.

##### Share on other sites
jake_Ghost    103
Rayno, i did as u said but it conflicts with other parts of my code so i had to do it another way w/o using vectors unfortunatly.

##### Share on other sites
Fruny    1658
Quote:
 Original post by jake_GhostRayno, i did as u said but it conflicts with other parts of my code so i had to do it another way w/o using vectors unfortunatly.

You can get a pointer to the vector's data by writing &vec.front(). It's guaranteed to be equivalent to a dynamically allocated array.

##### Share on other sites
Zahlman    1682
Quote:
Original post by Fruny
Quote:
 Original post by jake_GhostRayno, i did as u said but it conflicts with other parts of my code so i had to do it another way w/o using vectors unfortunatly.

You can get a pointer to the vector's data by writing &vec.front(). It's guaranteed to be equivalent to a dynamically allocated array.

However, a vector of vectors won't be; it's a more complicated beast than that (for one thing, not necessarily rectangular) :) so you'll want to use a single vector<BYTE> I assume, and size it appropriately (1,048,576 elements).

##### Share on other sites
Fruny    1658
Quote:
 Original post by ZahlmanHowever, a vector of vectors won't be; it's a more complicated beast than that (for one thing, not necessarily rectangular) :)

It'll still be equivalent to a dynamic array... of vectors. Of course, I'm not a big fan of vectors of vectors, at least when it comes to implementing a basic 2D dynamic array. [smile]