### Segmentation fault in recursive binary search

Hello

For one of my labs I am to create a quarter search(similar to binary search but split into quarters instead of halves). This is called recursively and we must use pointers. This is the code I have so far But i receive a segmentation fault. I'm pretty sure this is during the recursive call area. Can anyone point out where my code goes bad?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````bool quadSearch(int *arr,int value,int length){ int half = length/2; int quarter = half/2; int threeFourths = half+quarter; if(value == *(arr+quarter)){ return true; } if(value == *(arr+half)){ return true; } if(value == *(arr+threeFourths)){ return true; } if(value < *(arr+quarter)){ int * ptrTemp = new int[quarter]; for(int i=0; i *(arr+threeFourths)){ int * ptrTemp = new int[quarter]; for(int i=0; i *(arr+half) && value < *(arr+threeFourths)){ int *ptrTemp = new int[quarter]; for(int i=0; i

Last edited on
You should return the return value of the calls to quadSearch.

You also need to handle the case when length == 0?

A little confused with what you mean return the return values? And yes I will take care of the length == 0 case
 A little confused with what you mean return the return values?

Every time you call quadSearch recursively you discard the return value and return false.
Ok so this is my new code it compiles but never returns true or false just 0

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````bool quadSearch(int *arr,int value,int length){ int half = length/2; int quarter = half/2; int threeFourths = half+quarter; if(length == 0){ return false; } if(value == *(arr+quarter)){ return true; } if(value == *(arr+half)){ return true; } if(value == *(arr+threeFourths)){ return true; } if(value < *(arr+quarter)){ int * ptrTemp = new int[quarter]; for(int i=0; i *(arr+threeFourths)){ int * ptrTemp = new int[quarter]; for(int i=0; i *(arr+half) && value < *(arr+threeFourths)){ int *ptrTemp = new int[quarter]; for(int i=0; i

any other tips how to make this work?
Last edited on
Yes. Pay attention to what your compiler is tellling you about that function not always returning a value.

You're still discarding the return values any time you call quadSearch recursively, but instead of returning false you're evoking undefined behavior by returning nothing from a function that requires a return value.

`return quadSearch(ptrTemp, value, quarter) ;`
Topic archived. No new replies allowed.