# Your Worst "Gotchas" ever in programming

This topic is 1801 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Semicolons are just as easy to miss even if you put the brackets in a different line.

Notice the squiggly under the semi-colon, and under something. Also notice that the code inside of the brackets is greyed out. That's because the tools are smart enough to detect that the code inside the brackets is never executed.

##### Share on other sites

Semicolons are just as easy to miss even if you put the brackets in a different line.

QFT, happened once or twice to me.
The good thing is that those kind of mistakes (with infinite while loops) are very easy to debug!
The bad thing was, I didn't yet know how to use a debugger.

@tstrimple: Sadly, my IDE of choice doesn't do that.

(I use QtCreator, which is fairly modern, and includes alot of C++11 syntax highlighting for things like lambdas and such)

Edited by Servant of the Lord

##### Share on other sites

Macros with more lines and semicolon? Used and a bracket-less one liner in if/for/whatever statements? Got me once. I had to come here with it then feel the facepalms

##### Share on other sites

For me it was before I learnt the use of arrays, I used to declare every variable independantly and used copy/paste a lot, I lose track of the number of times I forgot to change a number and had two values trying to store in the same variable. One simple number cam cause so many issues.

##### Share on other sites

The good thing is that those kind of mistakes (with infinite while loops) are very easy to debug!

Then you do it with an if statement and you go crazy as the program behaves strange and you can't tell why. Even worse is if instead of behaving strange it ends up behaving exactly the same as the previous build... You'll go insane trying to figure out what did you forget to make the condition true.

Also reminds me, somebody I know says that you should always get somebody else to look at your code because you will always read what you intended to write, not what you actually wrote.

##### Share on other sites

Semicolons are just as easy to miss even if you put the brackets in a different line.

Notice the squiggly under the semi-colon, and under something. Also notice that the code inside of the brackets is greyed out. That's because the tools are smart enough to detect that the code inside the brackets is never executed.

Is this VS 2012? It looks like C#.

##### Share on other sites

Semicolons are just as easy to miss even if you put the brackets in a different line.

Notice the squiggly under the semi-colon, and under something. Also notice that the code inside of the brackets is greyed out. That's because the tools are smart enough to detect that the code inside the brackets is never executed.

Is this VS 2012? It looks like C#.

I did say language and tools. ;)

Yes, VS 2012.

##### Share on other sites
Default arguments are fucking evil.
struct Base
{
Base(int value1, int value2, int* pvalue3 = NULL);
};

struct DerivedOne
{
DerivedOne()
: Base(1, 2, NULL)
{  }
};

struct DerivedTwo
{
DerivedTwo()
: Base(3, NULL)
{  }
};
Spot the bug.

##### Share on other sites

It looks like C#.

I did say language and tools. ;)

Which, incidentally, also solves ApochPiQ's bug in the post above this.

Edited by Servant of the Lord

##### Share on other sites

I did say language and tools. ;)

That the language still allows you to make that exact same mistake in exactly the same way doesn't make it look like an upgrade =P

##### Share on other sites

I did say language and tools. ;)

That the language still allows you to make that exact same mistake in exactly the same way doesn't make it look like an upgrade =P

True, the tools for the language are what's better.

Edited by tstrimple

##### Share on other sites

Oh gee.

• Implementing a reusable, thread-safe, intrusive copy-on-write base for classes (2 levels of thread safety required!!)
• Any undocumented or hard to find caveat to a windows api call. Ex: WatiForMultipleObjects in some threading situations
• Different STL optimization flags set in a single project. This caused random crashes in random places I spent a looong time "debugging" in code which was actually correct.
• MSVC producing the same .obj filename out of two separate source codes in separate directories which themselves have the same filename. UGH. Major linking problems.
• C++ implicit casting has nailed me hard more than once.

##### Share on other sites

Which, incidentally, also solves ApochPiQ's bug in the post above this.

Actually, the implicit NULL conversion to literal 0 in my code is a total red herring. The actual evil is squarely with default parameters, and can be trivially reconstructed even with all the parameters being of primitive non-pointer types.

##### Share on other sites

It looks like C#.

I did say language and tools. ;)

Which, incidentally, also solves ApochPiQ's bug in the post above this.
You know, I'm sure with a higher than default warning level and 'warnings as errors' I've seen the compiler catch this error before now (reports as a warning, warning as error fails the compile).

Now, it won't catch you when you make this mistake...
auto it = std::begin(someContainer);
auto end = std::end(someContainer);
bool found = false;
while(it != end && !found)
{
found = *it == searchItem;
}

Opps...

##### Share on other sites

@ApochPiQ: Then I fail, cause I can't spot it at all.

(Ignoring the lack of actual inheritance in the structs, which I presume isn't the bug)

Edited by Servant of the Lord

##### Share on other sites
I'll rephrase it slightly:
struct Base
{
Base(int oldValue, int newValueWeJustAdded, int oldValueWithDefault = 42);
};

struct EverythingMustBeOne : Base
{
EverythingMustBeOne()
: Base(1, 1, 1)
{  }
};

struct EverythingMustBeTwo : Base
{
EverythingMustBeTwo()
: Base(2, 2)
{  }
};
Assume that Extremely Bad Things™ happen if the value of oldValueWithDefault in EverythingMustBeTwo is not 2.

##### Share on other sites

You know, I'm sure with a higher than default warning level and 'warnings as errors' I've seen the compiler catch this error before now (reports as a warning, warning as error fails the compile).

Gcc requires -Wall and -extra to catch it. Clang catches it by default.

(incidentally, this discussion is occurring over here as well).

##### Share on other sites

Now, it won't catch you when you make this mistake...



auto it = std::begin(someContainer);
auto end = std::end(someContainer);
bool found = false;
while(it != end && !found)
{
found = *it == searchItem;
}

Opps...

Lesson: stick to for loops when doing that kind of iterations.

##### Share on other sites

Lesson: stick to for loops when doing that kind of iterations.

I prefer the 'while' construct; imo it better expresses the intent of the loop.
'for' tends to imply you'll hit a whole range where 'while' is more a 'while these conditions are true' and, in my mind, is better for a search.

Would have prefered to have used an std algorithm however, would have been clearer and without the mistake...

##### Share on other sites

http://www.gamedev.net/topic/617629-works-with-x64-debug-x64-release-and-x32-debug-but-not-with-x32-release/

If werent by gamedev...Ive had gave up on programming many times..(learned new stuff in that one)

Edited by Icebone1000