negative index in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • destination
    New Member
    • Dec 2009
    • 34

    negative index in array

    say i have a two dimensional array a[512][512]. There is one more 2D array
    b[512][512]. The algorithm is like this depending on value of a[i][j] i have compare b[j][k] with two of its neighbour.
    This is a part of that algorithm
    if(a[j][k]==0)
    {
    if(b[j][k]>=b[j][k+1] && b[j][k]>=b[j][k-1])
    some code here
    }

    the problem is when k=0 b[j][k-1] will be b[j][0-1]=b[j][-1].
    To handle this i have padded b[i][j] in such a way that b[j][-1] and b[-1][k] is zero for all j and k.

    My question here is is it the right way to handle the negative indexes in array??
    should there be any modification made in the code.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    That is not the correct way to handle negative array indexes. Using a negative array index is undefined behaviour, it the computer will do it you will certainly have accessed memory outside of the boundary of the object.

    The correct solution is to re-write the code so that it does not make any attempt to access an array using a negative index.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      It depends on what your algorithm is trying to do, and if space/time is an issue. Building a [514][514] array requires 2052 more elements of space.

      Comment

      • destination
        New Member
        • Dec 2009
        • 34

        #4
        Banfa
        You said that it is not hte right way to handle negative indexes in array then what should be the right way to access a negative index.
        Take a simple example
        int a[10]={1,2,3,4,5};
        suppose i further declare a[-1]=0 and then use it in the code will that be a wrong thing to do?
        In the original question i have padded the array and then using it.Is that a wrong thing?

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          The point is that you must not declare a[-1]=0.

          You can either modify your logic to treat the first and last entries as special cases. Or instead of declaring the array with size N and using index values of 0 to N-1; you could declare the array with size N+1 and use index values of 1 to N, with index value 0 available for those rare cases when your algorithm accesses the element before the first element.

          Comment

          • destination
            New Member
            • Dec 2009
            • 34

            #6
            donbock
            I read in K&R so i thought its possible to do such thing
            This is quote from K&R
            "If one is sure that the elements exist, it is also possible to index backwards in an array; p[-1], p[-2], and so on are syntactically legal, and refer to the elements that immediately precede p[0]. Of course, it is illegal to refer to objects that are not within the array bounds."
            chapter 5 (5.3 Pointers and Array) paragraph just before start of 5.4.

            What do they mean by this statement?

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              In terms of the address space, if you also had a pointer to the middle of the array, you could go relatively backwards by inserting in a negative index.

              What does your algorithm do? Fundamentally, this should be your first question.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Originally posted by destination
                Originally posted by K&R, last paragraph of 5.3
                If one is sure that the elements exist, it is also possible to index backwards in an array; p[-1], p[-2], and so on are syntactically legal, and refer to the elements that immediately precede p[0]. Of course, it is illegal to refer to objects that are not within the array bounds.
                What do they mean by this statement?
                They are talking about the case where a pointer variable points into the middle of an array. In the following example, a[0] is 0, pa[0] is 2, and pa[-2] is 0.
                Code:
                int i;
                int a[10];
                int *pa = &a[2];
                
                for(i=0; i<10; i++)
                    a[i] = i;
                The index used with a can be any value between 0 and 9.
                The index used with pa can be any value between -2 and 7.
                As long as you honor these restrictions you will never access a location outside the bounds of the array.

                Comment

                Working...