Last active
March 21, 2024 15:28
-
-
Save rcythr/a65a022f5b892256c537c6542cf83e34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| int* getIntP(bool return_null) { | |
| if (return_null) return nullptr; | |
| return new int(3); | |
| } | |
| int main(int argc, char** argv) { | |
| if(int* i1 = getIntP(true)) { | |
| std::cout << *i1 << std::endl; | |
| } else if(int* i2 = getIntP(false)) { | |
| std::cout << *i1 << std::endl; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output from this program is:
The
else ifbranch accidentally usesi1instead ofi2due to a copy/paste error. The language allows the use ofi1in the else branch. Presumably to allow for checks other thannullptrchecks.IMO, this is a language bug because the benefit of this "feature" is low utility, but it creates an entire class of bugs like this one.