1

I'm passing an array in function argument and when I tried to access the last element using arr[-1] in the function's body I'm getting some garbage. Can someone explain me why is that so?

Here is the code:

#include<stdio.h>

using namespace std;

int binsearch(int nelement, int arr[],int maxsize)
{
    int low = 0;
    int high = maxsize;

    printf("%d",arr[-1]);
    return 0;       
}

void main()
{
    int arr[] = {1,2,3,4,5,6,7,8,9};
    int flag = 3;
    printf("%d\n",arr[8]);
    flag = binsearch(flag,arr,sizeof(arr));
}

And here is the output:

9
-858993460
alk
  • 69,737
  • 10
  • 105
  • 255
Hemant
  • 1,313
  • 17
  • 30
  • Negative indicies are already discussed here: http://stackoverflow.com/questions/3473675/negative-array-indexes-in-c – Darko Jan 22 '13 at 17:46
  • 1
    What's up with that code? You're using namespaces, which is a C++ feature; and using ``, which is C... You want to do C? Ditch the namespaces. – netcoder Jan 22 '13 at 17:54
  • Unless this code is for an embedded system, it is not valid and will not compile in standard C nor C++, for several reasons. – Lundin Jan 22 '13 at 19:13

3 Answers3

3

arr[-1] does not access the last element. Conceptually, it access the space before the first element, but the behavior is not defined.

To access the last element, use arr[nelement-1] or arr[NumberOfElements-1], depending on what you mean by the last element. (Are you considering the array to contain only nelement elements, or do you want the last element in the entire allocated array? If the latter, you will need to know the number of elements in the array in order to calculate the index of the last one.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • actually when I use python we usually do this to access the last element of array and when i read it into my school notes i found the same, that is why i got confused why it is showing me garbage value. Anyways thanks for clarifying it. – Hemant Jan 22 '13 at 17:45
  • 1
    Using `arr[maxsize-1]` will run into undefined behaviour for sure as well, as depending on the size of an `int` `maxsize` is twice or fours or eights times as large as the array carries `int` elements. – alk Jan 22 '13 at 17:57
  • @alk: Fixed; I mistook their `maxsize` for the maximum number of elements, rather than the array’s size in bytes. – Eric Postpischil Jan 22 '13 at 18:10
2

Array indicies shall be unsigned. Using a negative value as index to an array provokes undefined behaviour.


As a side note:

This code won't pass the number of (int) elements down to the function:

int arr[] = {1,2,3,4,5,6,7,8,9};
...
flag = binsearch(flag, arr, sizeof(arr));

As the sizeof operator returns the number of bytes used by arr.

To pass the number of ints the array provides you might like to used the following construct:

flag = binsearch(flag, arr, sizeof(arr)/sizeof(arr[0]));
alk
  • 69,737
  • 10
  • 105
  • 255
0

Since maxsize is sizeof(arr) and arr is an array of int - it is the (sizeof(int) * no_of_elements_of_arr)

it must be arr[(maxsize/sizeof(int)) - 1] in place of arr[-1]

arr[-1] is expected to print some garbage value

sr01853
  • 6,043
  • 1
  • 19
  • 39
  • 1
    Using `arr[maxsize-1]` will run into undefined behaviour for sure as well, as depending on the size of an `int` `maxsize` is twice or fours or eights times as large as the array carries `int` elements. – alk Jan 22 '13 at 18:01