demosthenesk <
demost...@gmail.com> wrote in
news:46f41353-0df7-480b...@z19g2000vbe.googlegroups.com:
> hi to all,
> i am a newbie to C++ and i want some help if you could ...
>
> i try to implement a recursive linera search algorithm for learning
> purposes but i get segmentation fault.
> here is the code.
>
> the problem appears when ULLI searchKey gets big values like 999999.
> for small values like 254 the program works.
[...]
> ULLI RecursiveLinearSearch(const ULLI * array, ULLI key, ULLI low,
> ULLI high )
[...]
> return RecursiveLinearSearch( array, key, low + 1, high );
> } // end function recursiveLinearSearch
The parameters for RecursiveLinearSearch() take ca 32 bytes, not even
accounting for any other function call overhead. Recursing 1 million
times as you do makes it ca 32 MB. This is way more than the default size
of stack segment in typical implementations (around 4 or 8MB in 64-bit
implementations), so there is no wonder you get segfaults.
C++ is just not meant for so heavy recursion, hundreds are OK, millions
are not. You need to redesign your algorithm as a loop-based one or use
some other (functional) language (which would make this transformation to
a non-recursive algorithm behind the scenes for you, incidentally). There
is just no reasonable way to do a million recursions in C++.
hth
Paavo