Sign in to follow this  
signal_

Build error involving C++ vector [Please Assist]

Recommended Posts

Hi, I am having a build error. Hopefully someone can help. The following is the error statement:
Quote:
C:\Dev-Cpp\projects_D\Mini_World_03_24_08_01_ALPHA\Makefile.win [Build Error] [room.o] Error 1
IDE: Dev - C ++ Language: C ++ I know, I know, use Microsoft or whatever, but not Dev-C++. Currently, I am switching to Microsoft, but please let me know if this is a Dev-C++ problem (compiling & linking) or is this a problem with my code. See code below. I create a struct called 'roomObj'. It contains some data for my game objects. I then create a 2D vector of roomObj called 'roomBox'. This vector has one dimension [XWorld] & the other dimension [YWorld], which are x and y dimensions of the world, respectively.
struct roomObj  {
               int xPos;
               int yPos;
               int lifeformIndex;
               int lifeform; 
               bool aliveObj;             
        };
        
std::vector<roomObj> roomBox [XWorld] [YWorld];

The above code compiles fine for the case of XWorld = 300 and YWorld = 100. But, if I want to increase the size of the world ... let's say << XWorld = 1000 and YWorld = 300>> ... I get the error quoted above. If I leave XWorld = 300 and YWorld = 100 and use the following code:
struct roomObj  {
               int xPos;
               int yPos;
               int lifeformIndex;
               int lifeform; 
               bool aliveObj;             
        };
        
std::vector<roomObj> roomBox [XWorld] [YWorld];
std::vector<roomObj> roomBox2 [XWorld] [YWorld];
std::vector<roomObj> roomBox3 [XWorld] [YWorld]; 

I also receive the quoted error message shown above. What gives? Is this a compiler/linker problem with Dev-C++? Or will this problem persist when I transition to Microsoft C++? I am planning to transfer to Microsoft C++ later today, but I am hoping that someone can lend some insight. It would be a disaster if I can only use XWorld = 300 and YWorld = 100, because I will be limited to small worlds. So that leaves two possible sources of the problem: 1. My code 2. Dev - C ++ So, experts, please help. What say you? Thanks in advance.

Share this post


Link to post
Share on other sites
You are not creating a 2-d vector. Instead you are declaring a 2-d array of vectors. Assuming a vector's size is 12 bytes, you are requesting 1000*300*12 bytes (more than 3 MB) of stack storage. The thing is that stack size for your program is normally smaller (around 1 or 2 MB).

A declaration of a vector of vectors (a 2-d vector) would look like this:

std::vector<std::vector<roomObj> > roomBox(xWorld, std::vector<roomObj>(yWorld));

That would create a vector containing xWorld vectors, each containing yWorld roomObj's.

Share this post


Link to post
Share on other sites
It's possible that the compiler can't handle that much data. Even if it did compile, I wouldn't be surprised if you recieved some sort of stack overflow error on program startup.

sizeof(std::vector<>) is typically about 12 bytes. 1000*300*12 = ~3.5mb. Stack size on Windows is only 1mb by default, I believe. If you want such large amounts of data, you may need to use dynamic allocation.

EDIT: Ninja'd.

Share this post


Link to post
Share on other sites
Thanks for the quick responses. Okay, I was afraid I was using up too much memory.

Quote:

You are not creating a 2-d vector. Instead you are declaring a 2-d array of vectors.


That is of course correct. I typed it incorrectly in the OP. I would like a 2-D array of vectors though. Apparently, I will not have this luxury. So, what is a better alternative?

Here are the specifics and what I would like to accomplish:

I have an Xworld x YWorld grid, a checkerboard, if you will. I have different game pieces. For example: dog, cat, and mouse. Let's say there are x dogs, y cats, and z mice in the game world. Each of these game pieces will have a corresponding 'roomObj'. => struct roomObj {int xPos;int yPos; int lifeformIndex;int lifeform; bool aliveObj; };

I need a way to store this information. Let's say I have a 5 x 5 grid for a world and there are 2 dogs, 1 cat, and 3 mice for game pieces. Let's say there are 2 mice at (x,y) = (0,0), 1 mouse and 1 cat at (1,1), one dog at (4,1), and one dog at (4,4), for example.

How can I accomplish this? Prior to both of yr posts I thought I should push the roomObj's onto the vectors at positions (0,0), (1,1), (4,1), and (4,4), where the vector at pos. (0,0)=> 2 members, (1,1) => 2 members, etc.

Basically, I need a way to have multiple roomObj's at any given point on the game world grid.

Maybe I am making this more complicated than it need be, but I am confused. I need a solution so that I can build larger worlds.

Perhaps, someone can offer a manageable solution if one understands what I am after. Thanks again.

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