I was making a simple min() function when I suddenly asked myself a question which got a bit more complex than I though
I created a valid function, and got the idea to omit a condition between the two numbers. Here is my function:
int min(int x, int y) {
if (x > y) {
return y;
}
else if (x < y) {
return x;
}
}
So it works perfectly for different numbers, if I put 2 and 3 inside, it'll return 2. But what happen when I put two time the same number? e.g. : 2 and 2
Neither 2 > 2 and 2 < 2 are valid, so it's just returning... nothing?
That what I though, I compiled the program (VS2015), got a warning about not testing every cases (normal), and when it runs... it outputs 2. Why?
After talking with people on it (and checking the ASM code for this function), someone checked with Valgrind what was happening, and as he told me, it appears that it could be a memory leak. I don't exactly know how it works, so why returning no value make it returning 2? Which 2 is returned?
I also tested if one of these conditions were true for some reason with a simple std::cout, but none of theme are true, so that's not something about "simplifying" with if (x > y) {...} else {...}
So what is really happening here?
[EDIT] I don't wanna know how to "fix" it, because it's pretty obvious to me. The question is, why am I getting 2? I know that there should be an else statement, but I was curious about what will happen without it