Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Infinite recursion from lack of a cast

Infinite recursion from lack of a cast

Scheduled Pinned Locked Moved Clever Code
tutorial
6 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #1

    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 a ulong instead, for example:

    public ulong Method ( ulong i )
    {
    ulong result = outcome of doing something worthwhile with i

    return ( result ) ;
    }

    Then I decided I might want to support longs 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 longs to ulongs" before calling Method.

    public ulong Method ( long i )
    {
    ulong result ;

    if ( i >= 0 ) 
    {
        result = Method ( **(ulong)** i ) ;
    }
    else
    {
        result = Method ( **(ulong)** 0 ) ;
    }
    

    return ( result ) ;
    }

    P U 2 Replies Last reply
    0
    • P PIEBALDconsult

      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 a ulong instead, for example:

      public ulong Method ( ulong i )
      {
      ulong result = outcome of doing something worthwhile with i

      return ( result ) ;
      }

      Then I decided I might want to support longs 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 longs to ulongs" before calling Method.

      public ulong Method ( long i )
      {
      ulong result ;

      if ( i >= 0 ) 
      {
          result = Method ( **(ulong)** i ) ;
      }
      else
      {
          result = Method ( **(ulong)** 0 ) ;
      }
      

      return ( result ) ;
      }

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #2

      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

      P 1 Reply Last reply
      0
      • P Paul Conrad

        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

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Exactly. It's not supposed to be recursive.

        P 1 Reply Last reply
        0
        • P PIEBALDconsult

          Exactly. It's not supposed to be recursive.

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • P PIEBALDconsult

            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 a ulong instead, for example:

            public ulong Method ( ulong i )
            {
            ulong result = outcome of doing something worthwhile with i

            return ( result ) ;
            }

            Then I decided I might want to support longs 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 longs to ulongs" before calling Method.

            public ulong Method ( long i )
            {
            ulong result ;

            if ( i >= 0 ) 
            {
                result = Method ( **(ulong)** i ) ;
            }
            else
            {
                result = Method ( **(ulong)** 0 ) ;
            }
            

            return ( result ) ;
            }

            U Offline
            U Offline
            unimatrics
            wrote on last edited by
            #5

            Is this a joke?

            R 1 Reply Last reply
            0
            • U unimatrics

              Is this a joke?

              R Offline
              R Offline
              Ryan Binns
              wrote on last edited by
              #6

              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"

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups