I am so happy

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

I am so happy

Post 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.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
AndrewBuckley
Member
Member
Posts: 95
Joined: Thu Jan 29, 2009 9:13 am

Re: I am so happy

Post 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.
User avatar
Minoto
Member
Member
Posts: 89
Joined: Thu May 12, 2011 7:24 pm

Re: I am so happy

Post 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.
Those who understand Unix are doomed to copy it, poorly.
User avatar
iansjack
Member
Member
Posts: 4683
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: I am so happy

Post by iansjack »

Perhaps you should consider using splint?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: I am so happy

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: I am so happy

Post 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 :mrgreen:

*ducks and runs*
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: I am so happy

Post 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 :mrgreen:
:D One of the many reasons I avoid graphics programming! Even better when you have geometry involved (x0,y0,z0,x1,y1,z1)...
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: I am so happy

Post 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.
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: I am so happy

Post 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
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: I am so happy

Post 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. :-)
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: I am so happy

Post 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.
User avatar
iansjack
Member
Member
Posts: 4683
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: I am so happy

Post 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). :)
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: I am so happy

Post by Kevin »

-Wshadow should have caught it. And you already have -Werror to make it an error.
Developer of tyndur - community OS of Lowlevel (German)
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: I am so happy

Post 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.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: I am so happy

Post 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.
Developer of tyndur - community OS of Lowlevel (German)
Post Reply