-
struct question...
I have this struct
Code:
struct X_Tile
{
bool Walkable;
char symbol;
bool Trigger;
long TriggerNum;
bool RunScript;
char scriptfile[32];
};
and I want to have a max size if 256*256, but if I do this
Code:
X_Tile tile[256][256];
I get a message that says the program have encountered a problem. I need to have every tile have independent variables though, because of the way I wrote my script engine, and other parts of the program.
I hope that someone can assist me. My only guess was that the memory usage, but then again a little struct like that isnt really that big. 200-300 bytes is my estimate.
-
This doesn't seem like it could cause that problem. Post the shortest code snippet that produces the error.
-
Code:
int main()
{
system("pause");
XT_Tile tile[128][128];
system("pause");
}
that works, but this doesnt
Code:
int main()
{
system("pause");
XT_Tile tile[256][256];
system("pause");
}
this causes the program to crash.
-
Well, the sizeof operator returns 44 for your structure with my compiler (MSVC++ 2003's default compiler) and 256 * 256 * 44 = 2 883 584 which is more than what have access to. I'd suggest using dynamic memory allocation instead. You might want to use auto_ptr<> or boost::shared_ptr<>.
-
On my system, the bool, char followed by bool would take 4 bytes, followed by another 4 bytes for the long, and 4 more for the bool, then 32 bytes for scriptfile, for a total of 44 bytes.
256 * 256 * 44 = 2.75 megabytes
My guess is that you are exceeding the amount of memory you can use on the stack (or free store, whatever). Shifting RunScript to be before TriggerNum might save you another 0.25 megabytes on my system (or perhaps an optimising compiler would do that for you anyway), but perhaps the correct approach is to use a vector of vectors, or other suitable container.
-
so if I cut sriptfile to 10 bytes, and changed the long to short, then this would save me 24 bytes correct? then if I use new to create the tile array, this will solve my problem correct? Thank you all for any assistance you have provided.
therefor putting me at 16 bytes of data instead of 44. 1 + 1 + 1 + 2 + 1 + 10. this will cut over all usage to 1 MB of memory. Also that struct is suppose to be a typedef, but I must have missed it in copy pasting.
-
Dont make code like that, now if you add a member to the struct later on again your program might start to crash again and you have no idea where it comes from cos you made a lot bigger program by that tmie
Use dynamic memory or use something more modern like std::vector.
-
I am using auto_ptr<> and I dont need 32 byte file names for tiles. I use std::vector's a lot, but ti doesnt seem needed here. Thank you
-
As laserlight said, local variables are stored on the stack. Unfortunately stack can't hold very much memory (this can be defined in the executable though, for example regedit.exe has 12 288 bytes of stack and notepad.exe 69632 bytes). So local variables are not a very good idea for holding other data than single values and pointers.
-
In conclusion, you could do either of those :
Code:
std::vector< std::vector< XT_Tile > > tiles;
tiles.resize(256);
std::vector< std::vector< XT_Tile > >::iterator it;
for(it = tiles.begin(); it != tiles.end(); it++)
{
it->resize(256);
}
// Or... you can use this but I don't know how to implement the rest..
std::vector< boost::shared_ptr< XT_Tile > > tiles
For some obscure (and unknown) reason, I don't like having a vector of vectors and I'd probably go with the latter option even though I hardly doubt it's better, but I still suggest you the first option.
-
> XT_Tile tile[256][256];
static XT_Tile tile[256][256];
should fix that problem then.