#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0

    Array bounds check


    I am doing a array bound check program in C. The method I took is to transfer the original array expression to a array checked expression, e.g. int a[10] --> int a[arraycheck(a,10)]. The function checkarray will check whether the subscript is out of bound. However, when I compile the program, the compiler will tell me 'a' undeclared (first use in this function).

    Here is the problem. The compiler need to know the subscript of the array to allocate its memory, but my program need to know the address of the array and its subscript and then check whether it is out of bound and return its value. How can I resolve the problem?
    Below is the souce code of my checkarray function:

    #include<stdio.h>
    struct arrayTable
    {
    char *name;
    int value;
    struct arrayTable *next;
    };
    typedef struct arrayTable arrayTable;
    static arrayTable *head = NULL,*p;
    static int arraycheck(void *s1,int value);
    /*-----------------------------------------------------------------------*/
    /* array check function */
    /*-----------------------------------------------------------------------*/
    static int arraycheck(void *s1,int value)
    {
    arrayTable *newOne;
    arrayTable *current = head;
    while(current->next != NULL)
    {
    if(strcmp(current->name,s1) == 0)
    {
    if(s1 < 0 || s1 > current->value)
    {
    printf("Error, array is out of bound!");
    exit(1);
    }
    else
    return value;
    }
    else
    current = current->next;
    }
    newOne =(arrayTable*)malloc(sizeof(arrayTable));
    strcpy(newOne->name,s1);
    newOne->value = value;
    newOne->next = head;
    head = newOne;
    return value;
    };
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    First (and foremost) please enclose your code in "code" tags (see http://forums.devshed.com/misc.php?action=bbcode&s=).

    Second, if you are not comfortable working in C which allows you to write very fast and efficient code (though most people don't), why not try Java which does array bounds checking for you. You add lots of overhead for each single array dereferencing, and if your code is used often, you can significantly increase the time it takes to run.

    Third, what you are trying to do is really nonsensical. If you already know the boundary of your array (you are passing the maximum value in the call to your routine), just check it and leave the fancy stuff behind. If you are doing this for homework, tell your professor to pick better problems.

    Fourth, if you are interested in knowing if you have hit the end of your linked list (what it looks to me like you have), just create a known invalid value for your final element in your list and check that before proceeding.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    2
    Rep Power
    0
    Thanks to reply. However, I think there are some misunderstandings. First, I am doing a C language project, so I cannot use Java, though Java is a very good language.
    Second, I am doing the array bounds check program, so I need to record the address of the array and its upper subscript. Below is a detailed transfered source code:
    Code:
    Before:
    
    int arraycheck(void *s1,int value);
    int main()
    {
      int a[10],b[5];
      a[10]=5;
      b[4]=6;
      return 0;
    }
    
    After:
    
    int arraycheck(void *s1,int value);
    int main()
    {
      int a[arraycheck(a,10)],b[arraycheck(b,5)];
      a[arraycheck(a,10)] = 5;
      b[arraycheck(b,4)] = 6;
      return 0;
    }
    As you can see, if the program works, it will warn the programmer the a[10] is out of bound. It is the reason why I need to record the address of the array and its upper subscript and keep a link-list to record and trace the array bounds.
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Why?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    chennai
    Posts
    4
    Rep Power
    0
    to check the upper bound of an array use.

    sizeof(arrayname) .This will give the array's upper bound

IMN logo majestic logo threadwatch logo seochat tools logo