stupidest mistake in programming

Pages: 12
closed account (Dy7SLyTq)
so while im waiting for my natty iso to download, i came up with a question i dont think ive seen on this forum. whats the stupidest mistake you've made in programming? mine would have to be between when i wrote this loop:
1
2
3
4
5
while(SomeCondition); //<--- took me a whole day to find it
{
	/*some code that checked
	  something for me*/
}

it was meant to check if a robot was in a valid location on the map. kept hitting an infinite loop and couldnt figure out why :p

and when i got 50 errors, almost all of them complicated and confusing (i think it was when i was writing c code) and found the simplest one, which was that i had ommitted the type bool of the variable declariation, and magically all of the other errors were resolved after that <shudders>
I wouldnt say it was my dumbest mistake, but when i was just learning and making a program that would take numbers and give you its english spelling, i ran into an error that would give a strangely inaccurate number if the input was -1 and - 99. all other negative numbers worked fine, as well as all positive. it took me hours to find the cause, changing random things and testing them.

it turns out in one of the functions, i delcared "int i" but never defined it.
after changing that one line to "int i = 0" it worked perfectly, i never understood how it caused that much trouble, i might be able to figure it out if i looked at it now, alas i dont care enough

(its still the strangest thing to me considering num 1 - 99 worked while -1 - -99 didnt despite i converted them to a positive number before running them through the function )
Last edited on
After having a detailed read of the man page for gcc (again), I discovered some potentially useful warning flags that aren't enabled by -Wall -Wextra -pedantic. Hopefully the other compilers have something similar. The first of these is -Wuninitialized. I don't know what compiler you use Paoletti301 and I guess we all learn a lot from our mistakes, but this warning would of been a great deal of to you help back then. I mention it now because it could be really useful to a beginner to intermediate person who wasn't aware of it.

There are some others that I haven't really needed because I have been aware of the problems that coding causes, so I don't tend to write code like that:

-Wfloat-equal - equality comparison for floats
-Wconversion - conversion narrowing double to float, float to int etc, unless there is an explicit cast.
-Wswitch-default - if there is no default case
-Wmissing-include-dirs
-Woverloaded-virtual
-Weffc++ - std libs don't always conform to this

Hopefully this is helpful to someone. :+)
closed account (jwkNwA7f)
My code doesn't have bugs. All it has is some features of the programming language they forgot add to the compiler :)

To be more serious, I am not sure what is the stupidest mistake I have made.
closed account (N36fSL3A)
My code doesn't have bugs.
Goddamned liar.
closed account (jwkNwA7f)
@Lumpkin I was just joking. I was calling the bugs in my code just things that they forgot to add to the compiler. Typically everything I make has a few bugs in it. Some have a lot (most of them) .

EDIT: READ THE WHOLE POST
Last edited on
My code doesn't have bugs.
Goddamned liar.


Well technically no one's code has bugs, because it is impossible for small living creatures be inside software which is usually a series of magnetised spots on a magnetically plated surface.

EDIT: Grammar
Last edited on
closed account (jwkNwA7f)
@Script Coder lol
closed account (jwkNwA7f)
@Script Coder
Actually that is where the term "bug" came from. A long time ago (at least for me) when computers were huuge they had problems w/ them. Later, the people found out that bugs were crawling into the computers and messing them up.
@Script Coder
Actually that is where the term "bug" came from. A long time ago (at least for me) when computers were huuge they had problems w/ them. Later, the people found out that bugs were crawling into the computers and messing them up.


I know :)
Lol, I wonder if debugging back then was spraying insect repellent on the hardware :P

Back on Topic: I feel that every time I make a mistake it is my worst...
I really don't know what my most stupid mistake in programming is, I remember months ago spending 3 hours to fix a PHP script only to realise I made a typo in one of the variable names (despite isolating what line was causing the problem within 30 mins). For C++, I was making an RPG as my first project, I had a RNG for damage, I spent 2 days trying to find why HP wasn't going down the same amount as it said damage was being done. It was something like:

1
2
3
4
5
6
7
8
9
int Damage (int Strength, int Defense)
{
    return Strength*Strength/Defense * (0.8+(rand()%4/10));
}

// enter a lot of code here ....

cout << "You did " Damage (MainCharacter->Strength, monster[i].Defense) << " damage." << endl;
monster[i].CharHP-=Damage (MainCharacter->Strength, monster[i].Defense) ;


It took 2 days to realise that the 2 Damage function calls might not be the same number :)
Last edited on
closed account (S6k9GNh0)
When I started, I used to use either printf or std::cout for logging, depending on my environment. Anyways, I had built a rather in depth multi-threaded library which I was proud of... but it was strangely killing itself! I had printf statements all over the place to find them and found that the program was dying before certain log statements had been shot out...

For several days, I debugged, I added more log statements, and I was finally at the point of blaming the library I was using... After talking with some of the people who had created the library, I presented the entire code base and pointed out the areas of use of their library. I still couldn't find the problem and neither could they.

So, here's the problem: Let's take a quick log from the code: printf("Connect to IP: %s", address);

Strange enough, some of the log statements actually did work. I don't remember the exact reason why but because there is no newline in the format string above, the message is never flushed and thus never seen.

As a matter of fact, I had thought it was broken because of where the program had stopped but it was actually the debugger dying since the main thread was doing nothing but waiting for the others to finish (which I had set a timer on) that had then died while the other threads were still working perfectly fine. It had nothing to do with the library, nothing to even do with networking, strange external exit commands or parachute functions I wasn't aware of.

Sometimes the problem is as simple as it seems. The problem was some log statements weren't showing up. Ironically, it was the log functions causing the problems.
@computerquip wow, that is bad. What surprised me the most is that you even contacted the people who made the library and they found nothing. Also, I bet you have always used cout since then.

I am also glad to here that I am not the only one who uses couts instead of a debugger.
closed account (S6k9GNh0)
Actually, std::cout << can do the same thing! Although, I don't remember what determines this... safest bet is to flush whenever you want a message presented I suppose.

EDIT: Just read your last line. I used both and still use both. I use a logger for conventional reasons though. I usually have high amounts of verbose debug logs spread through out my code that can easily be turned off and on.
Last edited on
1
2
#include <cstdlib>
abs(float) /*instead of*/ std::abs(float)

I even wrote a whole algorithm for randomly deforming terrain, which relies on the error. I changed all occurrences to std::abs, and it no longer worked. So I changed it back to this -
std:abs(int(result)); and somehow this makes the difference between
long uniform spikes on a flat sheet vs rugged mountainous terrain.

The worst part is that it was the combination of this error, and another logic error which led to working algorithm.
Last edited on
closed account (3qX21hU5)
Just today I did kind of a stupid mistake on the current project I am working on. I had two classes and each class had a member of the other class and for some reason for awhile there I couldn't figure out what the heck was going wrong(Lack of sleep I keep trying to tell myself ;p). I actually thought it was VS playing games on me again, so about after a hour of messing around it finally donned on me that there was recursive definition going on (Which was why I was getting over a hundred errors ;p) and I felt like a idiot lol.

So ya I agree computerquip sometimes it is the little things that hang you up.
I've only ever fallen prey to technicalities and things I didn't know were undefined, my code usually either fails to compile or has missing parts I forgot to write.
Every single time I write some piece of code, there are always at least five typos.

But a few days ago, it took me quite some time to realize what was wrong with:
1
2
template <typename InIter, typename InIter>
void Foo(InIter curr, InIter end);

After I finally figured it out, I decided it was time to take a much needed break.


htirwin wrote:
std:abs(int(result));

Irony. :|
Noob Question:

@Daleth I do not really work with templates very much, so please excuse me for asking: What is wrong with that? Is it that you have named them the same thing?
Undefined behaviors I stumbled on so far:

1
2
3
4
5
6
7
char c = std::numeric_limits<char>::min();

do
{
    // use c for something
}
while (c++ != std::numeric_limits<char>::max());
GCC ignores this loop if optimizations are on.


1
2
3
4
const char c = std::numeric_limits<char>::min();

if (std::isalpha(c))
    // will it print?
May blow up for extended ASCII codes.


http://www.cplusplus.com/forum/general/89081/
http://www.cplusplus.com/forum/general/93112/
Last edited on
Pages: 12