c++ Binary search through recursion

0

I receive an error when the first recursice call is made, the error:

Unhandled exception at 0x002A2E44 in rekBinSearch.exe: 0xC0000005: Access violation reading location 0x0000000A.

It is caused by:

if ((*pEnd - pBegin) == 0) / There's only one element */

It seems that that when i set the new start- and end-adress, i do something wrong since these can't be read on the recursive call. They are "set" by:

find(x, (int*)pBegin, pMid);

Full code:

    bool find(const int x, const int* pBegin, const int* pEnd)
{
   if ((*pEnd - *pBegin) == 0) /* There's only one element */
    {
        if (x == (int)pEnd)    /* That element could be the correct one */
            return true;
        else                   /* If it is not then return false, x is not in the array */
            return false;
    }

    int *pMid = (int*)(pEnd - pBegin);  /* pMid should be the adress to the element in the middle of the array */
    if (x >= (int)pMid)                 /* If x is in array it is to the right of the middle */
        find(x, (int*)pMid, pEnd);  
    else                                /* If x is in array it is to the left of the middle */           
        find(x, (int*)pBegin, pMid);

}// find

What am I doing wrong or how am I thinking wrong?

c++
recursion
binary-search-tree
asked on Stack Overflow Oct 20, 2015 by MattiasLarsson • edited Mar 21, 2020 by Dharman

2 Answers

2

What am I doing wrong or how am I thinking wrong?

Problem 1

You are confusing between pointers and values. Examples:

if ((*pEnd - *pBegin) == 0) /* There's only one element */

and

if (x == (int)pEnd)

int(pEnd) does not get the value of the object that pEnd points to. It just treats the pointer value as an int.

Problem 2

Also, you are not returning properly from the recursive calls.

    find(x, (int*)pMid, pEnd);  // Missing return

and

    find(x, (int*)pBegin, pMid); // Missing return

Fixed up function

Here's a version that should work.

bool find(const int x, const int* pBegin, const int* pEnd)
{
   if ((pEnd - pBegin) == 0) /* There's only one element */
   {
      return (x == *pEnd);  /* That element could be the correct one */
                            /* If it is not then return false, x is not in the array */
   }

   int midIndex = (pEnd - pBegin)/2;
   int const* pMid = pBegin + midIndex; /* pMid should be the adress to the element in the middle of the array */
   if (x >= *pMid)                     /* If x is in array it is to the right of the middle */
      return find(x, pMid, pEnd);  
   else                                /* If x is in array it is to the left of the middle */           
      return find(x, pBegin, pMid-1);

}// find
answered on Stack Overflow Oct 20, 2015 by R Sahu • edited Mar 21, 2020 by Dharman
0

Do you want if ((pEnd - pBegin) == 0)? Notice that there is no dereference pointers. Dereferencing pend is always a bad idea since it doesn't point to anything.

answered on Stack Overflow Oct 20, 2015 by Robert Jacobs

User contributions licensed under CC BY-SA 3.0