Infinite recursion from lack of a cast
-
Here's a little bit of potential infinite recursion I wrote just half an hour ago. (I did catch it before I tried to run it, but it was a near thing.) Boiled down, I had a method that used a
long
but which wasn't supposed to accept negative values so I decided I wanted it to use aulong
instead, for example:public ulong Method ( ulong i )
{
ulong result = outcome of doing something worthwhile with ireturn ( result ) ;
}Then I decided I might want to support
long
s anyway, for backward compatibility, but treat negatives as 0:public ulong Method ( long i )
{
ulong result ;if ( i >= 0 ) { result = Method ( i ) ; } else { result = Method ( 0 ) ; }
return ( result ) ;
}I compiled it and was looking at it when it suddenly occured to me, "I guess I'd better cast those
long
s toulong
s" before calling Method.public ulong Method ( long i )
{
ulong result ;if ( i >= 0 ) { result = Method ( **(ulong)** i ) ; } else { result = Method ( **(ulong)** 0 ) ; }
return ( result ) ;
} -
Here's a little bit of potential infinite recursion I wrote just half an hour ago. (I did catch it before I tried to run it, but it was a near thing.) Boiled down, I had a method that used a
long
but which wasn't supposed to accept negative values so I decided I wanted it to use aulong
instead, for example:public ulong Method ( ulong i )
{
ulong result = outcome of doing something worthwhile with ireturn ( result ) ;
}Then I decided I might want to support
long
s anyway, for backward compatibility, but treat negatives as 0:public ulong Method ( long i )
{
ulong result ;if ( i >= 0 ) { result = Method ( i ) ; } else { result = Method ( 0 ) ; }
return ( result ) ;
}I compiled it and was looking at it when it suddenly occured to me, "I guess I'd better cast those
long
s toulong
s" before calling Method.public ulong Method ( long i )
{
ulong result ;if ( i >= 0 ) { result = Method ( **(ulong)** i ) ; } else { result = Method ( **(ulong)** 0 ) ; }
return ( result ) ;
}Is there more to your recursive function? I keep getting stack overflow. It never reaches the base case :)
Some people have a memory and an attention span, you should try them out one day. - Jeremy Falcon
-
Is there more to your recursive function? I keep getting stack overflow. It never reaches the base case :)
Some people have a memory and an attention span, you should try them out one day. - Jeremy Falcon
Exactly. It's not supposed to be recursive.
-
Exactly. It's not supposed to be recursive.
It is just a real screwy subtle bug :-D --modified However, C++.NET does not compile the equivalent code unless you cast the parameter. It complains of making an ambiguous call.
Last modified: 12mins after originally posted --
If you try to write that in English, I might be able to understand more than a fraction of it. - Guffa
-
Here's a little bit of potential infinite recursion I wrote just half an hour ago. (I did catch it before I tried to run it, but it was a near thing.) Boiled down, I had a method that used a
long
but which wasn't supposed to accept negative values so I decided I wanted it to use aulong
instead, for example:public ulong Method ( ulong i )
{
ulong result = outcome of doing something worthwhile with ireturn ( result ) ;
}Then I decided I might want to support
long
s anyway, for backward compatibility, but treat negatives as 0:public ulong Method ( long i )
{
ulong result ;if ( i >= 0 ) { result = Method ( i ) ; } else { result = Method ( 0 ) ; }
return ( result ) ;
}I compiled it and was looking at it when it suddenly occured to me, "I guess I'd better cast those
long
s toulong
s" before calling Method.public ulong Method ( long i )
{
ulong result ;if ( i >= 0 ) { result = Method ( **(ulong)** i ) ; } else { result = Method ( **(ulong)** 0 ) ; }
return ( result ) ;
}Is this a joke?
-
Is this a joke?
unimatrics wrote:
Is this a joke?
Your reply? Yes.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"