Array boundary checking in C

This is a discussion on Array boundary checking in C within the C Programming forums, part of the General Programming Boards category; Hi All prog 1: Code: int main(void) { int i; int arr[3]; for(i=0;i<4;i++) scanf("%d",&arr[i]); for(i=0;i<4;i++) printf(" %d",arr[i]); } INPUT: 1,2,3,4 ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    25

    Array boundary checking in C

    Hi All

    prog 1:
    Code:
    int main(void)
    {
        int i;
        int arr[3];
        for(i=0;i<4;i++)
            scanf("%d",&arr[i]);
        for(i=0;i<4;i++)
            printf(" %d",arr[i]);
    }
    INPUT: 1,2,3,4
    OUTPUT: 1,2,3,4

    prog 2:
    Code:
    int i;
    int arr[3];
    int main(void)
    {
        for(i=0;i<4;i++)
            scanf("%d",&arr[i]);
        for(i=0;i<4;i++)
            printf(" %d",arr[i]);
    }
    INPUT: 1,2,3,4
    OUTPUT: 1,2,3,3

    As C never checks for boundary, we can insert values in an
    array even its boundary is crossed, but why do I get different output
    if I declare int arr[3] and int i within & out side of main.

    Why dint I get segmentation fault?
    please comment

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    When you overstep the boundary of your array, your program will do one of two things:

    1) Corrupt other data that your program has access to.
    2) Attempt to corrupt data that your program does not have access to.

    If the second option occurs, then your program will hopefully suffer a segmentation fault and die. On some operating systems, this action is allowed, leading to all kinds of unintentional behavior.

    Since your program does not crash, and you're hopefully on a system that does protect memory, the chances are that you are corrupting other memory that your program is allowed to overwrite. In the first example, i and arr are variables that are allocated on the stack. In the second example, i and arr are "global variables". That could simply just mean they are found in the .data segment of your program.

    This means you are most likely writing into very different locations in your program's allocated memory in both examples. Either way, it's wrong to do (as I'm sure you already know).

    http://www.howstuffworks.com/c23.htm

    This article provides a scenario about overstepping the bounds of two arrays and what bugs occur from doing so.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    25
    thanks for your valuable comment....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-20-2005, 11:39 PM
  5. Replies: 5
    Last Post: 05-30-2003, 12:46 AM

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