• embed_me@programming.dev
      link
      fedilink
      arrow-up
      6
      ·
      39 minutes ago

      I don’t get your point. Just because the image gets repeated in the 4th panel doesn’t mean its recursion. It can be an infinite while loop with a state.

      Now that I think about it, a recursion without a base/break condition is just an infinite loop with a state

      • AllHailTheSheep@sh.itjust.works
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        28 minutes ago

        I think modern compilers do actually compile recursion to be equivalent with an iterative model.

        edit: yes when possible they will compile to be iterative, but if it can’t be written iteratively it will be a series of calls and returns. depends on the specific type of recursion it looks like.

  • CaptPretentious@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    3 hours ago

    It’s saddening to me how many times I’ve actually come across this in production.

    Just because the code works doesn’t mean it should ship.

  • henfredemars@lemdro.id
    link
    fedilink
    English
    arrow-up
    12
    ·
    edit-2
    7 hours ago

    The compiler (in C) is allowed to assume that infinite loops eventually terminate. This can lead to these kinds of loops not actually running forever when built with an optimizing compiler.

    ISO/IEC 9899:2017 §6.8.5 “Iteration statements”, paragraph 6:

    “An iteration statement may be assumed by the implementation to terminate if its controlling expression is not a constant expression, and none of the following operations are performed in its body, controlling expression or (in the case of a for statement) its expression-3: – input/output operations – accessing a volatile object – synchronization or atomic operations."

    It can, for example, simply optimize it away, assuming non-productive infinite loops are stupid and not reflective of what the code will actually do.

    • azertyfun@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      2 hours ago

      if its controlling expression is not a constant expression

      Pretty big caveat. If I’m reading this right true definitely qualifies as a constant expression and the loop in the meme would therefore not be optimized away.

      • henfredemars@lemdro.id
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 hour ago

        There’s also this part of the standard that throws a wrench into this hypothesis:

        §5.1.2.3/4: (Program execution, Observable behavior):

        Accesses to volatile objects and calls to library I/O functions are observable behavior. The implementation may perform any transformation of a program, provided that the resulting program’s observable behavior is not changed.

        So it seems that running forever isn’t an observable property that must be preserved when code is transformed.

        Still, I think compilers try to not surprise the developer too badly and would recognize a trivial loop most of the time.

    • marcos@lemmy.world
      link
      fedilink
      arrow-up
      5
      ·
      6 hours ago

      for example, simply optimize it away

      Yeah, that example makes it reasonable. But the optimizer can do ridiculous stuff when it proves the loop never terminates and also assume it terminates.

      The most famous example of UB bullshit is when some compilers run code that is impossible to reach just because there’s an infinite loop on the file (not even in the same function).

      • henfredemars@lemdro.id
        link
        fedilink
        English
        arrow-up
        6
        ·
        5 hours ago

        The lovely part about UB is it’s non-causal. The compiler can go back in time and steal Halloween candy from you when you were five and still comply with the specification.

  • pageflight@lemmy.world
    link
    fedilink
    English
    arrow-up
    37
    ·
    9 hours ago

    Cute — though the visual gag fits a little better with infinite recursion that infinite loop.

  • Triumph@fedia.io
    link
    fedilink
    arrow-up
    18
    ·
    9 hours ago

    Long ago, I made a four line recursive HTML that did this with frames. Browsers didn’t have protection against that at the time, so if you opened it and let it run, your machine would lock up.

    • io@piefed.blahaj.zoneOP
      link
      fedilink
      English
      arrow-up
      11
      ·
      8 hours ago

      not sure but for it to be a fork bomb you need something like & in ur pseudo code to go on to the next call

      rn the first call of f within the loop never compiles so the second doesn’t happen. So this is “just” infinite recursion

      pls don’t scream at me should i be wrong.

    • palordrolap@fedia.io
      link
      fedilink
      arrow-up
      3
      ·
      6 hours ago

      On my computer, this pushes one core to ~60%, eats ~40MB of memory over the course of about a minute and then segfaults.

      I did make one small change to the condition which would mean that it would bail out if available memory got too low, but 40MB barely even registered so it was basically true the whole time. In retrospect, I probably should have been monitoring process count instead (or done both), but I guess I got away with it.

      As OP says, you need to create subprocesses with & to cause real problems.

      *Bash 5.2.15 / LMDE6 / who knows what other factors. Try these things at your own risk. Or better, just don’t.

  • schnurrito@discuss.tchncs.de
    link
    fedilink
    arrow-up
    4
    ·
    7 hours ago

    You can have a non-infinite loop without a break statement, you just need a return statement in it. Also for(;;) is much faster to write than while(true).

  • unalivejoy@lemmy.zip
    link
    fedilink
    arrow-up
    3
    arrow-down
    1
    ·
    edit-2
    6 hours ago

    Sometimes break is undesirable. But if you want an infinite loop, please please please include a sleep. Not doing this is why my CPU stays at 100%.