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.
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 »

Kevin wrote: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.
Neither D nor C# allow this particular construct.

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 »

When you use functions, especially in libraries, it is fairly obvious why you might want to reuse variable names in an inner scope. I personally wouldn't do so in quite the manner done in your example within a single function, but at least it is very visible there.

Perhaps the language designers just thought it was easier to produce compilers that didn't have to reject such use of local variables. It's not something that has caught me out in many years of programming, unlike some of Cs other little snares.
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:When you use functions, especially in libraries, it is fairly obvious why you might want to reuse variable names in an inner scope. I personally wouldn't do so in quite the manner done in your example within a single function, but at least it is very visible there.

Perhaps the language designers just thought it was easier to produce compilers that didn't have to reject such use of local variables. It's not something that has caught me out in many years of programming, unlike some of Cs other little snares.
Its not something I have ever noticed before, despite reasonably extensive experience with C, which is why I was suprised to see it accept it. Actually the bug in my code was slightly less obvious that the example provided, which was just a simplistic reduction of the problem (I had a construct like for(uint32_t i = 0; (i < cluster_size) && (ptr < buf_size); i++, ptr++) or something similar and then a for(int i = 0; i < ...; i++) nested somewhere inside it as part of a fat driver).

I would have thought, however, that it is easier to design a compiler that rejects this redefinition rather than writing extra code which has to decide which version of 'i' to update at each point?

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 »

Personally I feel that declaring the variable within the for loop in this way is likely to lead to this sort of error. (Although it's not an error as such. The code is doing what it is asked to, it's just not necessarily what the programmer intended.) The same really holds with declaring variables anywhere in the middle of a program. I prefer to declare them all at the start of a function or block. But that's just a style that works for me.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: I am so happy

Post by bluemoon »

To OP: You can turn on warning about shadow variables on gcc and prevent this kind of mistake happen again.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: I am so happy

Post by dozniak »

jnc100 wrote:I would have thought, however, that it is easier to design a compiler that rejects this redefinition rather than writing extra code which has to decide which version of 'i' to update at each point?
Supporting it this way needs no extra code apart from your regular scope and frame allocation. Catching the duplicate variable name reused in an inner scope requires adding a scan of all the outer scopes for the same variable name.
Learn to read.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: I am so happy

Post by Love4Boobies »

since u guyz good with style mby u can review my code ???? is c89 so easy to compile with gcc compilers

Code: Select all

#include <unistd.h>

main(v,c)char**c;<%for(c++<:v]="hello, world!\n)";(!!c)[*c]&&(v--||--c&&execlp(*c,*c,c[!!c:>+!!c,!c));**c=!c)write(!!*c,*c,!!**c);return 0;%>
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: I am so happy

Post by dozniak »

Love4Boobies wrote:since u guyz good with style mby u can review my code ???? is c89 so easy to compile with gcc compilers
It is c99, so no. (Or c89 with 1994 amendment, which makes it not exactly c89).
Learn to read.
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 »

@Love4Boobies:

Copying tutorials blindly can be a little misleading. You should contact the author of this fine C tutorial, Jack Aplin. :wink:

You should also note, for further reference, that reproducing a work without reproducing the copyright notice is an action that may lay you open to litigation.
Copyright (c) 1985, Landon Curt Noll & Larry Bassel.
All Rights Reserved. Permission for personal, educational or non-profit use is
granted provided this this copyright and notice are included in its entirety
and remains unaltered. All other uses must receive prior permission in writing
from both Landon Curt Noll and Larry Bassel.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: I am so happy

Post by Love4Boobies »

dozniak wrote:It is c99, so no. (Or c89 with 1994 amendment, which makes it not exactly c89).
Not as far as I can tell. Which post-C89 constructs do you see?
iansjack wrote:Copying tutorials blindly can be a little misleading.
What tutorial?
iansjack wrote:You should contact the author of this fine C tutorial, Jack Aplin. :wink:
I haven't heard of this person before. The program I've pasted seems to be a variation of his:
  • It includes the appropriate header.
  • It takes advantage of the fact that a and i[a] mean the same thing.
  • It uses digraphs for obfuscation.
  • It returns 0, which C89 does not do by default (unlike C99).
  • It doesn't come with other files.
  • It prints "hello, world!\n" rather than "Hello, world!\n".


Interesting find.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
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 »

Which post-C89 constructs do you see?
wikipedia wrote:In 1994 a normative amendment to the C standard, included in C99, supplied digraphs as more readable alternatives to five of the trigraphs. They are:

Code: Select all

Digraph 	Equivalent
<:         [
:>         ]
<%         {
%>         }
%:         #
Last edited by Combuster on Tue Feb 26, 2013 5:40 am, edited 1 time in total.
"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
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: I am so happy

Post by Love4Boobies »

D'oh! The answer was in my own reply, only a few lines below. Thanks.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
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 »

Actually, on my 64-bit Gentoo system both programs compiled but only printed the initial "h" (or "H") and then segfaulted. One day when I'm really bored I may debug it to see why. I tried running splint on it; for some reason it didn't like it!
Post Reply