Followers 0

# Your Worst "Gotchas" ever in programming

## 69 posts in this topic

semi-colon at the end of an if statement.

Why are you ignoring the state of the flag?? Why do you keep going into that code block? WHY??

Maybe the code isn't synced with the debugger! I'll rebuild. Nope, same problem. Stupid compiler/debugger/toolchain!

0

##### Share on other sites

pretty much anything i've done involving c++ has been riddled with gotchyas, I hope I never have to use that language again

0

##### Share on other sites

This took me back about 4 years, when I was working on my capstone uni project. Me and a friend were getting linker errors - something along the lines of " somevariable already defined in somefile.obj". No compilation errors - all our headers were #ifndef guarded, so we were scratching our heads for near four days.

The obvious thing, in retrospect, is that #ifndef guards only stop code declaration from happening twice. If you (as we did) put actual data in a header file (say, something like int someCount; that is not static), it doesn't matter if the .h file is guarded, when its included multiple times the linker will try allocate the variable again, and will fail, giving an error like above.

This happened to me as well, I was banging my head on the desk after looking over header guards for the n'th time, and then finally discovered what was going on with google's help (I was touching C++ for the first time). It was especially puzzling since I didn't have variables, but utility functions inside the header - doing so obviously worked for class methods, as they were inside the class declaration, but not for free functions. Took me something like three-four days to figure it out as well.

0

##### Share on other sites

In PHP, when I realized 0 == "pizza".

0

##### Share on other sites

In PHP, when I realized 0 == "pizza".

Is this the sort of thing that makes people say "TRWTF is PHP?"
0

##### Share on other sites
POKE 649, 0

Full Disclosure: I had to look that one up, my memory is not that good (disables the keyboard on the C64)

The latest one that tripped me up was when learning the OpenTK API & expanding my OpenGL fu, I could not get my shader to draw anything. Tried everything (seemingly) obvious (disabled culling, depth testing, simplified the shader to bare minimum, spot checks for errors, ran through gDebugger, etc.)

After beating my head against it long into the night, the next morning before work I had a moment of clarity and realized the obvious - I was doing the view-proj multiplication in the wrong order. Yep, 10 seconds later 'twas fixed.
0

##### Share on other sites

Another one: it took me three days with Visual Basic scripting for a NI software. Totally random crashes on my machine, unable to reproduce on others (but it was hard to try on other platforms), some emailing with the Support, when it turned out it was a bug in their software, my code was just fine.

The bug (memory corruption) was caused by a feature, that's behaviour was changed in the very version of the software I had. I guess they didn't debug it yet...

0

##### Share on other sites

I was using Racket the other day and searching a string with some regex, trying to find a backslash followed by a particular set of characters in a sub-capture group. I had the regex "\\(some|subcapture|group)" and it wouldn't catch things it should've matched, like "\a" (which, escaped would be like "\\a"). I figured out faster than I expected to that "\\(" actually means "a single escaped backslash that is escaping the parenthesis" and the correct regex I wanted was really "\\\\(some|subcapture|group)" (just to match something like "\a"). It wasn't long until all my regexes became a mess of backslashes...

Some recent ones for me have been working with Android, where everything is pretty much an asynchronous event system, but only certain functions can be called from certain threads (or perhaps two events can happen simultaneously and call the same function and cause conflicts), where I'd get various exceptions regarding thread conflicts or "not called from the main/GUI thread" exceptions. Grrrr.

I had a fun one the other month. We were working on a C project (most of the code was written by my boss so I wasn't familiar with it) that kept randomly crashing. It was so sporadic that I was guessing we were trashing memory somehow, but it was totally unclear where. I was the only one working with the code too, because my boss moved on to other stuff. Our investor/client wanted the prototype to show to other potential investors to raise capital, but the program was simply unstable. Anyway, I finally found the bug (after my boss had been digging through the code for hours): in one point, an array was being allocated with calloc (which takes the size of each element and the number of elements in an array to create) and reallocated with realloc (which just takes the total size in bytes, not the number of elements or the size of each element). The code was passing the number of elements to realloc (instead of the total number of bytes), as if it were calling calloc, so instead of growing the array it was shrinking it and we'd eventually trash the heap as the program went on. Honestly, I don't even think calloc should exist. Inconsistent calling semantics do no one any favors.

Ooooh, and another one. Signals. Everyone learns about using signals in C in school, and they always show how to use the signal() function to register callbacks. What everyone forgets to mention is that the signal() function is more or less deprecated and it is recommended to not use it because its effects vary across UNIX implementations. Plus, it's got undefined behavior in multithreaded apps. Well, our program was multithreaded and despite registering for callbacks, our program would still get killed from signals we should've been catching. It took some googling to find sigaction(), which is the "proper" way to do it that actually handles multithreaded programs. Screw you signal(), and may people stop teaching others about you.

Edited by Cornstalks
0

##### Share on other sites
JSON requiring quotes around keys when JavaScript doesn't. Gets me once every couple of weeks.
0

##### Share on other sites

In PHP, when I realized 0 == "pizza".

Is this the sort of thing that makes people say "TRWTF is PHP?"

Yes. Javascript has some funny ones too, and don't get me started on languages where null compared to anything is null (because all operations with null return null), resulting in hacks like isnull() and such. And in case you're curious about that piece of code: "pizza" is converted to an integer, and since it has no digits in it, it turns into 0 =/

As for gotchas: once I was filling a buffer using a pointer (i.e. *ptr++ = ...). Problem: the buffer got resized by calling realloc as needed as more data was processed. Guess what happened when the memory block was moved around when reallocated. It was so bad that the debugger couldn't tell where the error happened and valgrind outright crashed. Had to use printfs and place breakpoints around to figure out what line was crashing.

At least that was an easy fix: instead of doing *ptr++ = ...; do ptr[pos] = ...; pos++;

0

##### Share on other sites

Just a quick addition, since I have little time.

Symptom code:

int i = 0;

printf( "i = %d", i );

Output:

"i = 1"

It took four senior coders to work out what was going wrong here. The issue was overflowing buffers -> memory corruption. Causing data to not be assigned the value it was assigned. That was a fun monday morning.

I was debugging a student's code that had a very similar problem.  He declared an array of size 4, and accessed index #4 later in the code.  Unbeknownst to him (and me), that rewrites the value of another variable rather than throw an exception.  Took me a couple hours and several couts to spot the issue.

0

##### Share on other sites

It probably didn't throw an exception because it was technically writing to valid memory. It would only throw an exception when it tries to write to an address without any memory assigned. Same goes for reading.

And yeah, C arrays have that issue, it's done for performance reasons (not checking the size of the array every time it's accessed, which in turn involves knowing the size of the array too). Remember C was made when computers were pretty slow. It was probably better to do the checks only when needed (e.g. if you're handling a range of data you could do the check once outside the loop) than every time you accessed the data.

C++ vectors also have this catch, surprisingly: [] doesn't do boundary checks. To do boundary checks you need to use the at() member function. So e.g. instead of doing vector[4] you'd do vector.at(4). Fun stuff.

0

##### Share on other sites

All of them made me a better programmer, and I wouldn't want to not have made them. :)

0

##### Share on other sites

Yes. Javascript has some funny ones too

Yeah. Like (undefined = 4) evaluates to 4, but (null = 4) throws an error as you'd expect. Or the following:

function foo(a,b) {
return
a + b;
}


foo(3,4) returns undefined, because a semicolon is automatically inserted after the return keyword.

0

##### Share on other sites
When I was refactoring my OpenGL code to use OpenGL 3.2 and SDL 2, my code broke. All my GL calls were succeeding, but the screen stayed black. An OpenGL debugger told me that I had a 0 by 0 framebuffer, but I was completely sure that it was giving me a bogus value.

At least a month later:

Turns out I'd forgotten to initialize some class members in my constructor. My framebuffer was indeed 0 by 0. The fix was quite easy. :)
0

##### Share on other sites

semi-colon at the end of an if statement.

Why are you ignoring the state of the flag?? Why do you keep going into that code block? WHY??

Maybe the code isn't synced with the debugger! I'll rebuild. Nope, same problem. Stupid compiler/debugger/toolchain!

This. So annoying.

0

##### Share on other sites

I wrote a function to apply blurring to an image stored in a 2D array passed in (VS2010, C++). The function had a local 640x480 2D array on the stack. Calling the function caused really strange things to happen and had me baffled while trying to change random things to make it better. In the end, it turned out it was a stack overflow, with the additional array sending it over the top of the default 2MB size. That was a weird one. It would be nice if something indicated the problem in debug mode.

0

##### Share on other sites

I once typed this in some multi-threaded server code and spent an hour trying to figure out why things were not working.

while(server->active);{
// Not important.
}


.. in case you can't see it.. look at the extra ';' and what it creates in the generated logic.

0

##### Share on other sites

in my first text adventure i made a casino function that  was supposed to be a place payers could spend their money with an opportunity to make more and use that for upgrades stat boosts, etc.  however i will always remember that function because that was the first time i encountered a rogue curly brace.  I kept running the function and it kept outputting the same exact results, which made the entire structure pointless especially since it was supposed to be a gambling area.  i studied that function for about a week before i decided to just read the entire thing line by line and discovered that by me missing one curly brace the entire function , barring one case,  was ignored by the compiler....... to this day i consider the rogue curly brace my greatest enemy ......

0

##### Share on other sites

I will probably get the your a dummy look.

Worked on a game with a group of people using Flixel. Was good but we needed a few things that FlashPunk offered instead. Ported all the code over to FlashPunk. Decided to mock up some UI in good ol Flash oldschool style. Linked everything together and.. voila. White Screen.

Tried again day after day after day after day. We were all stumped on why the project file wasn't working.

That day I learned I was somehow able to link a .as3 file to a .as2 FLA. I still do this day have no idea how I was able to do it. However when the problem was fixed it still was a white screen.

Stepping through everything we relized we were all a bunch of dummys. Line 3 started the Flixel engine. We never started FlashPunk. Everytime we started it we did isolated tests with Flashpunk with some Copy Pasta.. man I felt dumb.

0

##### Share on other sites

I once typed this in some multi-threaded server code and spent an hour trying to figure out why things were not working.

while(server->active);{
// Not important.
}


.. in case you can't see it.. look at the extra ';' and what it creates in the generated logic.

This is a big part of the reason I never open my brackets on the same line as my conditionals.

0

##### Share on other sites

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

0

##### Share on other sites
memset (&mywonderfulstruct, 0, sizeof (&mywonderfulstruct));


The evils of copy-and-paste, and it's so bloody subtle; thankfully I noticed it right away but damn - it was close.

0

##### Share on other sites

I once made a switch() with a few cases in it, then I decided I'd prefer it as a series of if's and else's (I needed more control)...but I left the 'break' statement in one of the if's...then copied and pasted that if a few times, with the 'break;' still there...

In short: bye 30 minutes, I'm sorry I didn't make much out of you

0

## Create an account

Register a new account