an impossible bug

This is a discussion on an impossible bug within the C++ Programming forums, part of the General Programming Boards category; ok, this one has me completely stumped. somehow a variable is getting set by a block of code that doesnt ...

  1. #1
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    an impossible bug

    ok, this one has me completely stumped. somehow a variable is getting set by a block of code that doesnt even mention that variable. take a look:

    Code:
    board[0].x = BoardPosX;
    board[1].x = BoardPosX;
    
    for ( int i = 39; i < 53; ++i )
    {
    	deck[i].val = i - 39;
    	deck[i].suit = 3;
    	deck[i].X = i * 12;
    	deck[i].Y = 10;
    	deck[i].selected = FALSE;
    	deck[i].played = FALSE;
    	deck[i].overturned = FALSE;
    }
    
    assert ( board[0].x == BoardPosX );
    assert ( board[1].x == BoardPosX );
    these asserts both fail every time. i explicitly set the values equal to what i want to be, but every time that for loop is run, they get changed. i put some break points and ran it through the debugger, and i watched it magically change the wrong values. somehow on the last run of the for loop, deck[i] ( in this case, deck[52] ) is effecting the first two indexes of board.

    is it possible for the OS to screw up and allocate two different arrays in the same chunk of memory? neither of the arrays are dynamic, and my memory usage is normal. i really cant figure this one out...
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Because deck[52] is indexed by 0..51

    > for ( int i = 39; i < 53; ++i )
    This indexes deck[52] on the last iteration, and thus probably blows away something you didn't want to
    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.

  3. #3
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    oh man, i just realized that i put that in there when i was expecting to have a joker in my deck, but i didnt know how to draw a joker so i left it out for now.

    i totally forgot that i did that, thanks for pointing that out.


    which leads to another question... does anyone know how to draw a joker with cards.dll?
    Last edited by ...; 10-19-2003 at 04:29 PM.
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Kernel bug? (attn matsp)
    By brewbuck in forum Linux Programming
    Replies: 7
    Last Post: 04-13-2009, 10:31 AM
  2. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 02:47 PM
  3. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 12:56 PM
  4. Another link from Microsoft about bug in fread
    By vart in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-06-2008, 11:56 AM
  5. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM

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