Connecting Tech Pros Worldwide Help | Site Map

how I can debug this with gdb?

rolo
Guest
 
Posts: n/a
#1: Jul 22 '05
Im trying to do a recursive algoritm and debug it. My problem is that I got
segfault if I run it withous optimizing it with the compiler, since I want
to debug it. If I use a smaller number it runs ok, but im interested on
bigger numbers.

Can anyone help me out and explain why this happens?
heres a sample code: (it doesn't matter if I use template or not, I got the
same results)

If I optimize it with -O3 or -O2 the program runs fine and returns the
expected 0.

#include <iostream>

using namespace std;

template <class T>
inline T one_less(T tmp){
if(tmp > 0) return one_less(tmp - 1);
else return 0;
}//one_less(T tmp){

int main(void){
unsigned long int tmp = 100000;
cout << one_less(tmp) << endl;
cin.get();
return 0;
}//main(void){




~ Let us linux ~


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Artie Gold
Guest
 
Posts: n/a
#2: Jul 22 '05

re: how I can debug this with gdb?


rolo wrote:[color=blue]
> Im trying to do a recursive algoritm and debug it. My problem is that I got
> segfault if I run it withous optimizing it with the compiler, since I want
> to debug it. If I use a smaller number it runs ok, but im interested on
> bigger numbers.[/color]

Indeed.[color=blue]
>
> Can anyone help me out and explain why this happens?
> heres a sample code: (it doesn't matter if I use template or not, I got the
> same results)[/color]

Sure. Consider the code below. How many recursive invocations of
`one_less' are there going to be?

You're likely `blowing your stack' (hitting an OS limitation).[color=blue]
>
> If I optimize it with -O3 or -O2 the program runs fine and returns the
> expected 0.[/color]

<OT>
Your compiler (in optimizing mode) evidently performs an optimization
called `tail-call elimination' which replaces a certain class of
recursive calls by a simple loop. I would advise you to search the web
for more details.
</OT>[color=blue]
>
> #include <iostream>
>
> using namespace std;
>
> template <class T>
> inline T one_less(T tmp){
> if(tmp > 0) return one_less(tmp - 1);
> else return 0;
> }//one_less(T tmp){
>
> int main(void){
> unsigned long int tmp = 100000;
> cout << one_less(tmp) << endl;
> cin.get();
> return 0;
> }//main(void){
>[/color]
HTH,
--ag
--
Artie Gold -- Austin, Texas
Closed Thread