Thread: Trouble with Array indexes

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Trouble with Array indexes

    I am working on porting a program from C to Java. I did not write the original code, but there are several instances where the original programmer references [-1] as an array index. It is ALWAYS in a multi-dimensional array and it is ONLY in the first field for the array. For example:

    Code:
    for (i=0; i < numRows; i++)
    	    for (j=0; j < numCols; j++)
    	    {
    	      za[i][j] = mapConf[-1][i][j];
    	      h[i][j] = za[i][j]; 
    	      if (h[i][j] < sealevArray[0]) h[i][j] = sealevArray[0];
    	      qx[i][j] = 0.;
    	      qy[i][j] = 0.;
    	    }
    }
    Obviously, Java gives ArrayIndex errors, but apparently C permits it.

    Can anyone tell me how to translate that? Does it really mean the first index in the array or is there something exotic happening with the pointers?

    This application does work in the original C. It was originally developed with the C compiler shipped with SGI's in case that makes a difference.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It does not mean the first index. The first element of an array in C (and Java) is always at index 0. C may "permit" it, but it will result in undefined behavior, meaning something harmless, something awful or nothing at all may happen. It has similar effects to using an index that is too big, except here you run off the beginning of the array instead of the end.

    It may be possible that the original programmer overloaded the [ ] operator to take a negative index to mean "from the end of the array", which is an idiom used in other languages. That would make za[-1][i][j] the j'th column of the i'th row of the last "page" of mapConf, if used in that context.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In C, you can't access -1. You get 0 through SIZE-1. If your array is:
    Code:
    int array[ 3 ];
    Then:
    Code:
    array[ -1 ] = crash my program please!
    array[ 0 ] = valid
    array[ 1 ] = valid
    array[ 2 ] = valid
    array[ 3 ] = crash my program please!
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    The suggestion that the original programmer was looking for the last index in the array makes sense. I will look for the operator overload configuration. If that was the intention I should be able to replicate it in Java.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Doh! I misread your first post. If you're porting from C to Java, then there is no way the original programmer overloaded the [ ] operator in C. There is no overloading in C (only in C++, but you said this was C so...). Negative indexes are invalid, period. The SGI compiler may not check this, or you may not have the warnings turned up high enough. Basically, array[i] is the same as *(array + i), so it could happily convert array[-1] to *(array - 1). Like I said, this is in the realm of undefined behavior meaning anything could happen, including your program "working fine", especially if the memory layout has you indexing into some spot used for alignment/padding. Nevertheless, it's wrong and needs to be corrected.

    The only other possibility I can think of right now is that mapConf actually points into the middle of an array (with a "guarantee" that there is at least one "page" before where it points), thus mapConf[-1] refers to the previous page from where it's pointing. That would be one nasty, ugly hack, and I sincerely hope that isn't the case.

    You need to determine and validate the original programmer's intentions, and handle this correctly in Java.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by anduril462 View Post
    Doh! I misread your first post. If you're porting from C to Java, then there is no way the original programmer overloaded the [ ] operator in C.
    That's what I thought he was doing as well.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I don't see anything wrong with *(array - 1) or array[-1] if the pointer 'array' is such that it points to a place in memory which has values preceding it. Pointer math does allow subtraction for a reason.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    I don't see anything wrong with *(array - 1) or array[-1] if the pointer 'array' is such that it points to a place in memory which has values preceding it. Pointer math does allow subtraction for a reason.
    Unless the array pointer is someplace in the middle of another array... going negative on indeces will mostlikely result in garbage results. It might even accidentally change the value of other variables or the return pointer on the stack... Not a risk worth taking unless you know exactly where that pointer is aimed and what is right before it...

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Negative indices can be valid, but they are generally suspicious. For example (with a one-dimensional array)

    Code:
    void func(int x[])    /*  pointer argument */
    {
         x[-2] = 42;
    }
    
    int main()
    {
         int arr[5];
         func(arr + 2);
    }
    In this case, since x (in func()) points at arr[2] (in main()), x[-2] is the same as arr[0].

    That said, the circumstances where one wants to do such things in practice are not all that common. So negative indices should be viewed with suspicion - even if they sail past the compiler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting Indexes
    By satty in forum C Programming
    Replies: 2
    Last Post: 08-09-2010, 01:11 AM
  2. Replies: 1
    Last Post: 03-09-2010, 03:31 PM
  3. Negative array indexes
    By jw232 in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 08:32 PM
  4. MySQL Indexes
    By LuckY in forum Tech Board
    Replies: 1
    Last Post: 05-09-2007, 06:04 PM
  5. Sort indexes of frequency array?
    By davidol in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 09:04 AM