Sign in to follow this  
jake_Ghost

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by jake_Ghost
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.


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 this post


Link to post
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 array
HeightMap = new BYTE[1024*1024];



Share this post


Link to post
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 this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by jake_Ghost
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.


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 this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Fruny
Quote:
Original post by jake_Ghost
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.


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 this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by Zahlman
However, 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]

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