Overwriting all in array, why?

This is a discussion on Overwriting all in array, why? within the C Programming forums, part of the General Programming Boards category; Bugging the life out of me. I'm searching through an array (board) looking for patterns that match what's in bends[][][][] ...

  1. #1
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179

    Overwriting all in array, why?

    Bugging the life out of me. I'm searching through an array (board) looking for patterns that match what's in bends[][][][]
    Code:
    enum Direction bends[NUMBENDS][2][2][2] = {      /* [type][before/after][y][x]*/
    {{{ NONE, DOWN},{ NONE,  END}},{{ DOWN, LEFT},{RIGHT,  END}}},
    {{{ NONE, NONE},{  END, LEFT}},{{ DOWN, LEFT},{  END,   UP}}},
    {{{  END, NONE},{   UP, NONE}},{{  END, LEFT},{RIGHT,   UP}}},
    {{{RIGHT,  END},{ NONE, NONE}},{{ DOWN,  END},{RIGHT,   UP}}},
    {{{ NONE,  END},{ NONE,   UP}},{{RIGHT,  END},{   UP, LEFT}}},
    {{{ NONE, NONE},{RIGHT,  END}},{{RIGHT, DOWN},{   UP,  END}}},
    {{{ DOWN, NONE},{  END, NONE}},{{RIGHT, DOWN},{  END, LEFT}}},
    {{{  END, LEFT},{ NONE, NONE}},{{ END, DOWN},{   UP, LEFT}}}
    };
    
    ...skipping to the problem bit ...
    
        index = 0;
        for (xy = 0; xy < (MAX * MAX) - MAX; xy++) 
        {
          if ((xy + 1) &#37; MAX == 0) continue;
    
          for (c = 0; c < NUMBENDS; c++) 
          {
            if ((bends[c][0][0][0] != END) && (bends[c][0][0][0] != board[xy])) continue;
            if ((bends[c][0][0][1] != END) && (bends[c][0][0][1] != board[xy + 1])) continue;
            if ((bends[c][0][1][0] != END) && (bends[c][0][1][0] != board[xy + MAX])) continue;
            if ((bends[c][0][1][1] != END) && (bends[c][0][1][1] != board[xy + MAX + 1])) continue;
    
            mods[index][0] = xy; mods[index][1] = c; index++;
    for (d = 0; d < index; d++) mvprintw(debug++,40,"InBends%d:%d,%d",d,mods[d][0],mods[d][1]);refresh(); /*debug output*/
          }
        }
    I added a debugging loop there to output the array as it find it, and I'm getting some odd output. I'd expect it to show me a building array, but instead I get something like:
    Code:
    InBends0:27,4
    InBends0:29,6
    InBends1:29,6
    InBends0:37,7
    InBends1:37,7
    InBends2:37,7
    See how every time the array builds all previous entries are overwritten by the last one. Why? The loops and conditionals all look good to me. The problem is definately within those bracket, but I can't, for the life of me, figure out where.
    Last edited by guesst; 10-08-2008 at 03:50 PM.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your code could be formatted a little more clearly... crikey... Are you sure your debug loop isn't just being skipped?

  3. #3
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Yeah, sorry about the spaghetti code. Lemme go back and prettyfy it.

    The debug loop is being hit because of the tag on it that reads "InBends". That's why I put it there.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The code is too incomplete to be able to say anything about what may be going wrong. Generally, what you see is caused by overwriting the end of some other data, but who knows why that happens.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    I tried to include all of the code that would be necessary. The whole thing is riddled with bugs at the moment, but this is the once I'm trying to clear before anything else. So I've stripped down the program so that it only shows the error. Check it out:
    Code:
    #include <curses.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAX 3
    #define NUMBENDS 8
    
    enum Direction {NONE, UP, LEFT, DOWN, RIGHT, END};
    enum Direction board[MAX * MAX];
    int start, end;
    
    enum Direction bends[NUMBENDS][2][2][2] =       /* [type][before/after][y][x]*/
    {
    {{{ NONE, DOWN},{ NONE,  END}},{{ DOWN, LEFT},{RIGHT,  END}}},
    {{{ NONE, NONE},{  END, LEFT}},{{ DOWN, LEFT},{  END,   UP}}},
    {{{  END, NONE},{   UP, NONE}},{{  END, LEFT},{RIGHT,   UP}}},
    {{{RIGHT,  END},{ NONE, NONE}},{{ DOWN,  END},{RIGHT,   UP}}},
    {{{ NONE,  END},{ NONE,   UP}},{{RIGHT,  END},{   UP, LEFT}}},
    {{{ NONE, NONE},{RIGHT,  END}},{{RIGHT, DOWN},{   UP,  END}}},
    {{{ DOWN, NONE},{  END, NONE}},{{RIGHT, DOWN},{  END, LEFT}}},
    {{{  END, LEFT},{ NONE, NONE}},{{ END, DOWN},{   UP, LEFT}}}
    };
    int step[4] = {-MAX, -1, 1, MAX};
    
    int debug=0;
    
    void init ()
    {
        initscr ();
        noecho();
    }
    
    int change () 
    {
      int xy, c, d, index, mods[512][0]; /* mods[index][location/type] */
    
           /* Now we make a list of all possible modifications. First the bends...*/
    debug = 0;
        index = 0;
        for (xy = 0; xy < (MAX * MAX) - MAX; xy++) 
        {
          if ((xy + 1) &#37; MAX == 0) continue;
          for (c = 0; c < NUMBENDS; c++) 
          {
            mods[index][0] = xy; mods[index][1] = c; index++;
    
    debug++; for (d = 0; d < index; d++) 
    mvprintw(debug++,40,"InBends%d:%d,%d",d,mods[d][0],mods[d][1]);refresh();
          }
        }
        getch();
    
      return 0;    /* None of the modifications worked, so back up and try again. */
    }
    
    void start_generating () 
    {
      int c;
    
      for (c = 0; c < MAX * MAX; c++) board[c] = NONE;         /* Clear the board */
      start = rand() % (MAX * MAX);
      do {
        switch (rand() % 4) {
          case 0 : if (start - MAX >= 0) 
                  {
                     board[start] = UP;
                     end = start - MAX;
                  } break;
          case 1 : if (start % MAX) 
                  {
                     board[start] = LEFT;
                     end = start - 1;
                   } break;
          case 2 : if (start + MAX < MAX * MAX) 
                  {
                     board[start] = DOWN;
                     end = start + MAX;
                   } break;
          case 3 : if ((start + 1) % MAX) 
                  {
                     board[start] = RIGHT;
                     end = start + 1;
                   } break;
        }
      } while (board[start] == NONE);
      board[end] = END;
      change ();
    }
    
    int main () {
      init();
      getch();
      srand (time (NULL));
      start_generating ();
      return 0;
    }
    Okay, there it is a program you can compile yourself that illustrates the problem. Now what I expect (since there are no filters any more) is output like:
    Code:
    InBends0:0,0
    
    InBends0:0,0
    InBends1:0,1
    
    InBends0:0,0
    InBends1:0,1
    InBends1:0,2
    
    InBends0:0,0
    InBends1:0,1
    InBends2:0,2
    InBends3:0,3
    
    InBends0:0,0
    InBends1:0,1
    InBends2:0,2
    InBends3:0,3
    InBends4:0,4
    
    InBends0:0,0
    InBends1:0,1
    InBends2:0,2
    InBends3:0,3
    etc.
    Instead I get:
    Code:
    InBends0:0,0
    
    InBends0:0,1
    InBends1:0,1
    
    InBends0:0,2
    InBends1:0,2
    InBends2:0,2
    
    InBends0:0,3
    InBends1:0,3
    InBends2:0,3
    InBends3:0,3
    
    InBends0:0,4
    InBends1:0,4
    InBends2:0,4
    InBends3:0,4
    InBends4:0,4
    
    InBends0:0,5
    InBends1:0,5
    InBends2:0,5
    InBends3:0,5
    Illustrating that it's overwriting the entire array with the last thing put in it. You have the complete code, can anyone help?
    Last edited by guesst; 10-09-2008 at 10:21 AM.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    mods[512][0]; <--- woah!!!

  7. #7
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Quote Originally Posted by nonoob View Post
    mods[512][0]; <--- woah!!!
    Uh...

    Oh..

    Hey! Congratulations nonoob for being the eagle eyed coder who noticed that.. uh... intentional flaw in this program. Yeah. That's it. because I'd never do anything that stupid.

    Ug.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Glad to help. It helped that you put the rest of your code because I suspected the overwrite problem was elsewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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