Struct question

This is a discussion on Struct question within the C++ Programming forums, part of the General Programming Boards category; when the last member (down) is acsessed, it crashes. Code: struct key{bool left; bool right; bool up; bool down;}; Is ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    180

    Struct question

    when the last member (down) is acsessed, it crashes.

    Code:
    struct key{bool left;
               bool right;
               bool up;
               bool down;};
    Is there somthing wrong with how i declared this? All the other members work fine.

    DW

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    If something is going wrong during runtime, then no, everything is okay with that declaration. It is something wrong with how you are using it in your code. Post your code.

    If you are getting an error when you compile, post the error.
    My Website

    "Circular logic is good because it is."

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    It may be crashing because it was never initialized. Some compilers will do that, where others will just tell what happens to be in that memory block at that time.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by skorman00
    It may be crashing because it was never initialized. Some compilers will do that, where others will just tell what happens to be in that memory block at that time.
    no, if the program's crashing, it has little to nothing to do with the comper...

    I think what you mean is that some compilers will give you an error and halt compilation, where others will just compile the program with maybe a warning, and the program will give you the contents of that memory address...


    edit: this thread should never have been started: Mouvment problem
    Last edited by major_small; 06-01-2004 at 04:54 PM. Reason: not a memory address
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    >> edit: this thread should never have been started: Mouvment problem

    yup yup i know. i acidently clicked new thread, instead of post reply
    silly me. Sorry about that!

    Regardless, thats where the code is, and can anyone see anything wrong with it? it crashes during

    Code:
    void User_Laser::Collision(Enemy *Baddy[4][8], User_Laser *laser)
    {
      int i,k;
    
    
    
      for(i=1;i<5;i++)
      {
            for(k=1;k<9;k++)
            {
                  if(laser->y1 <= Baddy[i][k]->Baddy_Collision.left)
                  {
                        if(laser->Bullet.top >= Baddy[i][k]->Baddy_Collision.top &&  laser->Bullet.top <= Baddy[i][k]->Baddy_Collision.right && Baddy[i][k]->Dead == false)
                        {
                             Baddy[i][k]->Dead = true;
                             laser->Obsolete = true;
                        }
                  }
            }
      }
    }
    cheers, and sry again about the double post ;D

    DW

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I don't see where any key object is created or accessed...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    161
    Check your for loops. You're overstepping your array bounds.
    Code:
    for(i=1;i<5;i++)
    should be
    Code:
    for(i=0;i<4;i++)
    and
    Code:
    for(i=1;i<9;i++)
    should be
    Code:
    for(i=0;i<8;i++)
    Remember that arrays are indexed starting with 0 -- so an array with 4 elements would have indices 0-3. (not 1-4)

    -tf

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Looks like that's your answer deathwraith. Keep it in the same thread next time though.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    >>Keep it in the same thread next time though.

    Will do! Again, my bad.

    I changed the bounds of all my arrays( <5 to <4, <9 to <8, etc), and low and behold, it work! (imagine that!)

    But when i changed the bounds down by one, only a 3 by 7 array of baddies appeared, as opposed to a 4 x 8. how do i fix this?

    This is my current newing...

    Code:
      for(int k=1;k<5;k++)
      {
            for(int z = 1;z<9;z++)
            {
                    Baddy[k][z] = new Enemy(z,k);
            }
      }
    this is what I tried, but didn't solve the problem...
    Code:
      for(int k=1;k<6;k++)
      {
            for(int z = 1;z<10;z++)
            {
                    Baddy[k][z] = new Enemy(z,k);
            }
      }


    how do i change this so i get my desired 4 x 8 array again? thanks for the help!

    cheers

    DW

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    'k' and 'z' should start at zero thinks I:
    Code:
     for(int k=0;k<5;k++)
      {
            for(int z = 0;z<9;z++)
            {
                    Baddy[k][z] = new Enemy(z,k);
            }
      }
    I thought that's what you had changed.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    When I do that, should i keep the other counters from <5 && < 9 and starting at 1 or 0?

    DW

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    depends on how they are used. If the counters are used to keep track of how many times the body of the loop is done, then both ranges are equivalent and no biggy either way. However, if the counter is used as an array index, then you can get into trouble if the counter is out of range for the array.

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Example of looping through an array:
    Code:
    MyStruct array[20];    //There are 20 elements in the array
    
    for(int i = 0; i < 20; ++i)    //The loop looks like this.
    {
        //do something with array[i].
    }
    Note that i starts at 0, and the loop condition is "i < (size of the array)". Also note that this loop condition only lets the loop keep going until i reaches 19; 20 never gets run. So altogether 20 elements are checked, starting at 0 and ending at 19.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Registered User
    Join Date
    Jun 2004
    Posts
    14
    A general rule to keep in mind is that all true number systems start from zero. You will save yourself many headaches by following this rule.

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Yup, well that worked well. I'm just learning c++ coming from VB, so I forget every once and a while (read normaly ) that they start at 0! Cheers, and thx for the help!

Page 1 of 2 12 LastLast
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, 03:14 AM
  2. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-02-2007, 12:48 AM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM

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