struct question...

This is a discussion on struct question... within the C++ Programming forums, part of the General Programming Boards category; I have this struct Code: struct X_Tile { bool Walkable; char symbol; bool Trigger; long TriggerNum; bool RunScript; char scriptfile[32]; ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    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.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    This doesn't seem like it could cause that problem. Post the shortest code snippet that produces the error.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    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<>.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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.
    Last edited by Raigne; 05-21-2007 at 08:04 AM.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    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.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    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

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    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.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > XT_Tile tile[256][256];
    static XT_Tile tile[256][256];
    should fix that problem then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 02:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21