2

I have the following code that is doing a binary search based on array and value to look for that is passed to function. However, when I step into the function, the array has no values. Why is this happening? I have looked and I believe everything is correct in terms of passing the array.

 #include <iostream>
using namespace std;

int binSearch(int [], int , int , int);

void main()
{
    int a[10];
    int low;
    int high;
    int searchValue;

    for (int i = 0; i < 10; i++)
    {
        a[i] = i;           
    }

    low = 0;
    high = 9;
    searchValue = 6;
    int searchResult = binSearch(a, low, high, searchValue);
    cout << searchResult << endl;
}

int binSearch(int a[], int low, int high, int searchValue)
{
    int newHigh;

    newHigh = (low + high) / 2;

    if (low > high)
    {
        return -1;
    }

    else if (high == low)
    {
        if (a[high] == searchValue)
        {
            return high;
        }

        else
        {
            return -1;
        }
    }

    else if (a[newHigh] < searchValue)
    {
        return binSearch(a, low, newHigh, searchValue);
    }

    else
    {
        return binSearch(a, newHigh, high, searchValue);
    }
}
8
  • 3
    ... the array has no values ... How do you notice? Commented Mar 27, 2014 at 8:05
  • 2
    The array does have values, but your algorithm is wrong. Commented Mar 27, 2014 at 8:06
  • 1
    I am using visual studio debug and when I hover over the array int the main function before going into binSearch, I see that it has the values I put it in. I then step into binSearch and then when I hover over the array it has no values stored Commented Mar 27, 2014 at 8:13
  • 2
    The "array" in binSearch is not an array. It is a pointer. You need to change how you're inspecting the values. Commented Mar 27, 2014 at 8:15
  • 3
    Put a,10 in your watch window. Commented Mar 27, 2014 at 8:23

3 Answers 3

4

When you pass an array into a function it is said to "decay" into a pointer. Arrays in C++ are second class citizens and any time you pass them somewhere they are automatically converted into a pointer to the first element. This means that when you inspect the "array" inside the function, you're actually viewing the value of the first element (since an the array decays to a pointer to the first element).

One way you can inspect it as an array at this point is to add it to the watch window (right click -> Add Watch, or type the variable name in watch window) and put a comma next to it along with the size of the array.

For example typing a, 10 in the watch window while in your bin_search function would display the first 10 elements pointed to by a.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, that helped. I changed the argument to be (&a)[10] and it works perfectly. The algorithm just needs to be changed now as it is incorrect as pointed out above.
What do you mean by you "changed the argument to be (&a)[10]", I'm not quite certain I understand what you mean.
No, I made it int binSearch(int (&a)[10], int , int , int) This helped me understand the issue of why I thought it wasn't getting passed when in reality what was happening was the "decay" you described. I now realize, with your help and that of the answer below, that the real issue was with the algorithm in terms of why I wasn't getting the result I was supposed to.
@PeterClark: "when you pass an array into a function it is said to "decay"" - more precisely, it's when an array is passed to a function argument of type int*, combined with the Standard mandating that all int[] arguments be handled as int*s, that kicked in here to cause decay. You can pass an array into a function without decay if the function accepts it as such, e.g.: void f(int (&x)[4]) - but then the size must match perfectly (though template <size_t N> void f(int (&x)[N]) is legit). Need C.T. const size. Whether that would help VS debugger I don't know, but I suspect it would.
@TonyD The VS debugger does recognize using a reference to an array and it gives the desired results (just tested it with VS2012).
|
1

The array is fine. main() should return int in C++ and the binary search algorithm needed correction.

#include <iostream>
using namespace std;

int binSearch(int [], int , int , int);

int main()
{
    int a[10];
    int low;
    int high;
    int searchValue;

    for (int i = 0; i < 10; i++)
    {
        a[i] = i;
    }

    low = 0;
    high = 9;
    searchValue = 6;
    int searchResult = binSearch(a, low, high, searchValue);
    cout << searchResult << endl;
    return 0;
}

int binSearch(int a[], int low, int high, int searchValue)
{
    int mid;

    mid = (low + high) / 2;

    if (low > high)
    {
        return -1;
    }

    if (a[mid] == searchValue)
    {
        return mid;
    }    
    else if (a[mid] < searchValue)
    {
        return binSearch(a, mid+1, high, searchValue);
    }    
    else
    {
        return binSearch(a, low, mid-1, searchValue);
    }
}

2 Comments

Thank you so much! It was the algorithm all along that was giving me -1 because it was going into the wrong branch. However, I did also learn that that what is passed is pointer to first element, which is why I would see zero when I looked at it in debug.
for the else, it shouldn't be mid-1 in the recursive call, it should just be mid. This is because of the way integer division works and if you were to try searching for the middle it wouldn't work as it was before. Otherwise all works well, thanks.
0

just change int binSearch(int a[], int low, int high, int searchValue) to

int binSearch(int* a, int low, int high, int searchValue)

1 Comment

Both the function signatures you suggest are equivalent. Arrays are naturally decayed into pointers. Also, there is nothing wrong with using the array syntax to indicate that a parameter is an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.