Page 1 of 2
I am so happy
Posted: Tue Feb 19, 2013 11:06 pm
by BMW
I have just solved a problem in my OS that I have been unable to solve for about a week (I stopped doing OS Dev for a bit because of it).
Now, can anyone see anything wrong with this code?
Code: Select all
for(int j=0;i<amount;j++)
{
//code here
}
If you didn't, look again.
Scroll down for answer...
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
I mixed up Js with Is... in the for loop, I was declaring
j=0, checking if
i was less than amount and incrementing
j.... such a gay little problem. I am so happy now that I have solved this ***** of a problem. I can't believe it was something so stupid.
You may have seen the problem straight away, but in my text editor the I and the J look very similar.
Re: I am so happy
Posted: Wed Feb 20, 2013 12:39 am
by AndrewBuckley
using single variable names is not a good idea in general. using the j in the context of the for loop is fine, but if the compiler didn't complain about the i it means your using too small of a variable name for the length of its life. But thats just my opinion.
Re: I am so happy
Posted: Wed Feb 20, 2013 1:00 am
by Minoto
This is just a statement of my personal preferences and not an invitation to start an argument about style, but I don't mind using i, j, k, etc. for loop indices. The fact that I'm giving them those names rather than something meaningful means they aren't meant to be taken seriously outside of their immediate context as simple counters. However, I also think that separating identifiers from (most) operators in expressions makes it easier to catch things like this...to me, the out-of-place i certainly stands out more in the second example below than it does in the first.
Code: Select all
for(j=0;i<amount;j++) {
// do something
}
vs.
Code: Select all
for (j = 0; i < amount; j++) {
// do something
}
YMMV, of course.
Re: I am so happy
Posted: Wed Feb 20, 2013 1:35 am
by iansjack
Perhaps you should consider using splint?
Re: I am so happy
Posted: Wed Feb 20, 2013 1:52 am
by AJ
Hi,
BMW wrote:You may have seen the problem straight away, but in my text editor the I and the J look very similar.
Yes - even on phpBB, the problem does stand out clearly. I wonder if the font on your text editor needs changing (that's not sarcasm...)? Also, what compiler errors did you get, or had you also defined "i" as a variable? While I do use single letter indices on occasion, if you have nested loops, it is probably better to distinguish between them with meaningful names.
Cheers,
Adam
Re: I am so happy
Posted: Wed Feb 20, 2013 7:52 am
by Combuster
AJ wrote:if you have nested loops, it is probably better to distinguish between them with meaningful names.
For graphics: x, y, and z
*ducks and runs*
Re: I am so happy
Posted: Wed Feb 20, 2013 2:01 pm
by AJ
Combuster wrote:AJ wrote:if you have nested loops, it is probably better to distinguish between them with meaningful names.
For graphics: x, y, and z
One of the many reasons I avoid graphics programming! Even better when you have geometry involved (x0,y0,z0,x1,y1,z1)...
Re: I am so happy
Posted: Thu Feb 21, 2013 5:59 am
by Jezze
In those cases I really think it is ok to use x, y, z.
Also I'd like to add that i is fundamentally understood as being short for index so when you loop through an array and only an array that is fine too.
However, using j is not ok. in those cases you should probably use i0 and i1 just as with x0, x1 etc.
Re: I am so happy
Posted: Thu Feb 21, 2013 1:46 pm
by AJ
Jezze wrote:However, using j is not ok. in those cases you should probably use i0 and i1 just as with x0, x1 etc.
i, j and k are commonly used loop counter variables. That doesn't mean it's a good idea to use them.
I tend to use "i" for individual small loops. For anything larger or anything nested, I'd much rather use iBuffer, iSource, iDestination etc... For me, at least, that improves readability.
Cheers,
Adam
Re: I am so happy
Posted: Fri Feb 22, 2013 6:29 am
by sortie
Not sure such a trivial bug really deserves a topic, but I've encountered similar bugs recently and solved them rather quickly. Remember this bug and you won't spent a week on it next time.
Spoiler: most bugs are either because the code did something the programmer didn't intend or because the programmer intended the wrong thing. By that logic, most bugs are stupid.
Re: I am so happy
Posted: Sun Feb 24, 2013 4:44 am
by jnc100
I just came across a similar bug in my code, which can be reduced to the following:
Code: Select all
int main()
{
int foo = 0;
for(int i = 0; i < 20; i++)
{
for(unsigned int i = 0; i < 30; i++)
foo += i;
}
return foo;
}
It appears that gcc accepts this as valid C (using compiler options -std=c99 -pedantic -pedantic-errors -Wall -Wextra -Werror).
Is this valid C?? I appreciate I don't turn on all warnings but surely redefining 'i' in the loop (especially with a different variable type) should be an error rather than a warning?
Regards,
John.
Re: I am so happy
Posted: Sun Feb 24, 2013 5:31 am
by iansjack
Why would that be illegal? You are just defining a new variable with local scope and using a name that exists in the containing scope. Very bad style, sure, - the sort of thing you would expect in an obfuscating C entry - to reuse the variable name in this way but legal C.
And it gives the expected result (the sum of 0 to 29 multiplied by 20).
Re: I am so happy
Posted: Sun Feb 24, 2013 5:57 am
by Kevin
-Wshadow should have caught it. And you already have -Werror to make it an error.
Re: I am so happy
Posted: Sun Feb 24, 2013 6:34 am
by jnc100
iansjack wrote:Why would that be illegal? You are just defining a new variable with local scope and using a name that exists in the containing scope. Very bad style, sure, - the sort of thing you would expect in an obfuscating C entry - to reuse the variable name in this way but legal C.
And it gives the expected result (the sum of 0 to 29 multiplied by 20).
Very well, you live and learn, but seriously, apart from obfuscated C, what benefit does this 'feature' provide? Or is it something else to add to my long list of 'reasons not to use C'?
Kevin wrote:-Wshadow should have caught it. And you already have -Werror to make it an error.
Thanks, this has now been added to my warning list.
Regards,
John.
Re: I am so happy
Posted: Sun Feb 24, 2013 7:45 am
by Kevin
jnc100 wrote:Very well, you live and learn, but seriously, apart from obfuscated C, what benefit does this 'feature' provide? Or is it something else to add to my long list of 'reasons not to use C'?
It's a pretty common way of how programming languages work. In most of them you can shadow global variables with local ones. Off the top of my head I can't name a single one which supports different scopes and doesn't allow it.