Thread: why doesn't this work (confused)

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    27

    why doesn't this work (confused)

    I have an
    int rownum //which is globally defined

    in main I have
    Code:
      rownum = 0;
      test[0][0] = '[';
      test[0][1] = '1';
      test[0][2] = '7';
      test[0][3] = ']';
      test[0][4] = '\0';
      test[1][0] = 'b';
      test[1][1] = 'y';
      test[1][2] = 'w';
      test[1][3] = '\0';
      test[2][0] = 'r';
      test[2][1] = 'g';
      test[2][2] = 'b';
      test[2][3] = '\0';
      test[3][0] = 'r';
      test[3][1] = 'g';
      test[3][2] = 'b';
      test[3][3] = '\0';
    after the initialization of the 2d array test, rownum no longer equals zero. The only other place I use rownum is a function readline() which looks like this.

    Code:
    char* readline(){
      return(test[rownum++]);
    }
    I have no clue why rownum is becoming altered because I never call readline()

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What does rownum equal to when you try printing it?

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Also, how is the test variable declared?

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Would be nice to see all your code. If int rownum is trully getting altered they you have some sort of stack corruption occuring within your program. You will have to place some printf statements periodically throughout printing out the value of rownum from time to time to see what is occuring. Are you getting any compiler errors/warnings at all? It would be obvious of you are really hitting some memory boundaries though.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You should check that test is
    Code:
    test[4][5]
    or larger.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    27
    oh it might be cause my test initializations we're declared out of range of my declaration of test. But would that affect other variables?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If you overwrite a buffer that was declared on the stack, that will corrupt the stack. This means that other variables that were declared on the stack will have their values overwritten.

    Here is an example of what I mean. Run this program and look at the output:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i1 = 0;
        int i2 = 0;
        int i3 = 0;
        int i4 = 0;
        char buffer[0];
        int count;
    
        for(count = 0; count < sizeof(int) * 4; ++count)
            buffer[count] = count;
    
        printf("i1 = %d\n", i1);
        printf("i2 = %d\n", i2);
        printf("i3 = %d\n", i3);
        printf("i4 = %d\n", i4);
    
        return 0;
    }
    Last edited by bithub; 04-20-2009 at 08:07 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Absolutely! When you overrun the bounds of some variable, whatever you do will frequently impact some other variable.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    It doesn't matter whether variables are on some stack or not. They could be globally defined. The issue is that the compiler allocates memory for each variable someplace. Likely consecutive areas. When an array is accessed beyond its logical limits, the areas outside of its bounds are affected. There is usually no run-time checking of array bounds. Arbitrary pointer values or indices can end up reading or writing to memory that's designated part of user's data area at least - hopefully. Best case. But it may impact more strategically significant parts of the computer as well causing major crashes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. capture card wont work with xp
    By scott27349 in forum Tech Board
    Replies: 6
    Last Post: 02-08-2005, 09:47 PM
  2. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM
  3. Loading a BMP....it should work.
    By VirtualAce in forum Game Programming
    Replies: 12
    Last Post: 08-14-2004, 08:08 AM
  4. At Work...Need C++....
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 06-04-2004, 12:36 AM
  5. confused
    By sketchit in forum C Programming
    Replies: 1
    Last Post: 09-24-2001, 10:30 AM