Fellas, why is this happening, especially sometimes but not other times?

Here, I run the program three times, getting the exact same correct output. On the fourth, it outputs broken numbers. The function prnStats what’s being executed.

  • kevincox@lemmy.ml
    link
    fedilink
    arrow-up
    3
    ·
    2 years ago

    I would trying running with address and undefined behaviour sanitizers enabled. The results can often be surprising.

    Other than that look for entropy sources. Do you ever reference the current time, random numbers or anything else that could make a difference from one run to the next?

    • lofenyyOP
      link
      fedilink
      arrow-up
      1
      ·
      2 years ago

      Only a file that doesn’t change between runs. I posted a copy of the full source code in a different post here in this thread. It’s been minimised down to 105 lines. Still can’t figure out where the bug is.

      I’ll try your suggestion though, thank you!

    • lofenyyOP
      link
      fedilink
      arrow-up
      1
      ·
      2 years ago

      I tried -fsanitize=undefined and -fsanitize=unreachable with no luck. Are there other appropriate sanitizers I’m missing?

      • kevincox@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        2 years ago

        The uninitialized variables would have required -fsanitize=memory but that one can be hard to use because IIUC it requires recompiling all dependencies.

        However in this case you likely would have got a compiler warning. I recommend compiling with -Wall to start, you can then turn off a few warnings that are too noisy.

        • lofenyyOP
          link
          fedilink
          arrow-up
          1
          ·
          2 years ago

          Thanks for the help! I’ll definitely start using -Wall. I don’t know why I don’t do it by default…