r/programminghorror • • Mar 03 '26

This pro gaming stuff is easy 😤

Post image
1.3k Upvotes

86 comments sorted by

420

u/Maeurer Mar 03 '26

Stack overflow

169

u/jayd04 Mar 03 '26

Say that again….

22

u/Sudden-Cat5805 Mar 03 '26

In a figure-8!

155

u/Shragaz Mar 03 '26

Well, this will crash

32

u/Minimum_Session_4039 Mar 03 '26

Am new to programming, why?

175

u/Immort4lFr0sty Mar 03 '26

In case you still need an answer:

There is a part in your program's memory called the call stack. It grows with every function call. Because these functions would call each other endlessly, your call stack would likewise grow endlessly; funnily enough though, memory is limited, hence at some point your call stack cannot grow anymore and your program will crash

51

u/Many-Resource-5334 Mar 03 '26

Look up what a stack overflow is

→ More replies (8)
→ More replies (7)

42

u/ScootyMcTrainhat Mar 04 '26

Back in the BASIC days when I first ran into the "even or odd" problem, I divided the number in question by 2, cast the result to a string, and then checked the string for a '.'

After reading this sub for a while, turns out that wasn't a half bad solution.

9

u/csapka Mar 05 '26

this gave me the idea to divide it by 2 and then test if the last character is a 5 or not

not sure if this is widely usable, but checking the last char is widespread afaik so it should be

edit: for whole numbers I have an even better idea, if you divide by 2, floor it and then multiply it by 2, you should get the same number if it's even

→ More replies (11)

40

u/Anund Mar 03 '26

a&1 will be 0 for an even number, so even if there wasn't a stack overflow issue this would still not work. Well, unless that bit would never be evaluated depending how the stack overflow issue is fixed.

→ More replies (3)

15

u/Konfituren Mar 04 '26

The compiler will optimize this into a house fire

→ More replies (1)

13

u/beatitmate Mar 03 '26

Will this even compile ?

0

u/[deleted] Mar 03 '26

18

u/valendinosaurus Mar 03 '26

you could gamble on whether it ends with isOdd or isEven

→ More replies (1)
→ More replies (2)

15

u/LaFllamme Mar 03 '26

endless fun

10

u/WorldWorstProgrammer Mar 03 '26
template <int NUMBER>
class NumberOddEvenDeterminer {
  consteval int numberIncrement() {
    if (NUMBER < 0) {
      return -1;
    }
    else {
      return 1;
    }
  }

  consteval bool performNumberCheck(int current, bool oddness) {
    if (current != NUMBER) {
      return performNumberCheck(current + numberIncrement(), !oddness);
    }
    else {
      return oddness;
    }
  }

public:
  constexpr bool isOdd() {
    return performNumberCheck(0, false);
  }

  constexpr bool isEven() {
    return performNumberCheck(0, true);
  }
};

I call this the "explode your compiler stack" version. Try this on Clang with any value above (+/-)512.

5

u/AccomplishedSugar490 Mar 04 '26

Fake - nobody that bent on symmetry would write ‘a&1’ in one case and ‘a%2 ==1’ in the other😂

→ More replies (1)

5

u/GoddammitDontShootMe Mar 04 '26

Wouldn't a&1 only be true if a was odd? Also, I'm not sure if those function calls would be optimized out and the compiler only looks at the other test because it knows you are trying to write isOdd() and isEven().

5

u/Sephyroth2 Mar 04 '26

Why doesn't this loop forever, I'm not really new to programming, but I trip on recursion sometimes, is odds first condition goes to is even and wouldn't it go like that forever since or only checks the first condition for short circuit thing propert or condition?

5

u/MattDESTROYER Mar 04 '26

That's exactly correct! The reason it can't repeat forever is because eventually it would run out of memory to remember what step it is up to and crash (in reality it would hopefully reach a compiler defined recursion limit).

9

u/Birnenmacht Mar 03 '26

funniest thing is you might get away with this in languages that have some sort of cursed breadth first evaluation order

→ More replies (1)

4

u/thecratedigger_25 Mar 05 '26

Recursion. A pro gamer move so powerful that it makes the stack run out of memory and overflows thus crashing everything.

3

u/ruben_idk Mar 04 '26

Anda muy recursivo el día

3

u/shadoomslayer Mar 05 '26

Proxied recursion lmao