Home > Archive > C > December 2007 > Annoying compiler warning
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Annoying compiler warning
|
|
|
| Hi,
I have been trying to use some inventive alternative idioms for infinite
loops in my code, rather than the same old for(;;) and while(1) - this
could be a nice amusing Easter-egg for any future maintenance
programmers!
One of my ideas was this:
unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
while(++u>=0)
{
// loop
}
Unfortunately, the compiler gives a warning for this, which rather
spoils the fun... does the compiler have to give a diagnostic here, and
can anyone think of a way of using the same basic idea but without
generating a warning?
| |
|
| Bob wrote:
>
> Hi,
>
> I have been trying to use some inventive alternative
> idioms for infinite
> loops in my code, rather than the same old for(;;) and while(1) - this
> could be a nice amusing Easter-egg for any future maintenance
> programmers!
>
> One of my ideas was this:
>
> unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
> while(++u>=0)
> {
> // loop
> }
>
> Unfortunately, the compiler gives a warning for this, which rather
> spoils the fun...
> does the compiler have to give a diagnostic here,
If it did, I could look it up.
> and can anyone think of a way
> of using the same basic idea but without generating a warning?
How to suppress an unidentified warning?
That's not a bad one.
unsigned u = SHRT_MAX / 1000;
while(--u >= 0u)
{
// loop
}
--
pete
| |
| John Smith 2007-12-19, 7:58 am |
| Bob wrote:
> Hi,
>
> I have been trying to use some inventive alternative idioms for infinite
> loops in my code, rather than the same old for(;;) and while(1) - this
> could be a nice amusing Easter-egg for any future maintenance
> programmers!
>
> One of my ideas was this:
>
> unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
> while(++u>=0)
> {
> // loop
> }
>
> Unfortunately, the compiler gives a warning for this, which rather
> spoils the fun... does the compiler have to give a diagnostic here, and
> can anyone think of a way of using the same basic idea but without
> generating a warning?
>
What message do you get from the compiler ?
Works for me with #include <limits.h>
| |
|
| Bob wrote:
>
> Hi,
>
> I have been trying to use some inventive alternative idioms for infinite
> loops in my code, rather than the same old for(;;) and while(1) - this
> could be a nice amusing Easter-egg for any future maintenance
> programmers!
>
> One of my ideas was this:
>
> unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
> while(++u>=0)
> {
> // loop
> }
>
> Unfortunately, the compiler gives a warning for this, which rather
> spoils the fun...
> does the compiler have to give a diagnostic here, and
> can anyone think of a way of using the same basic idea but without
> generating a warning?
unsigned u;
for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)
{
// loop
}
--
pete
| |
| Richard Heathfield 2007-12-19, 7:58 am |
| pete said:
<snip>
> How to suppress an unidentified warning?
> That's not a bad one.
>
> unsigned u = SHRT_MAX / 1000;
> while(--u >= 0u)
I get a warning for that code:
foo.c:6: warning: comparison of unsigned expression >= 0 is always true
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
| |
|
|
| James Kuyper 2007-12-19, 7:58 am |
| Richard Heathfield wrote:
> pete said:
>
> <snip>
>
>
> Much better.
But still capable of triggering a compiler warning from a sufficiently
clever compiler.
| |
| Richard Bos 2007-12-19, 6:58 pm |
| Bob <no@spam.com> wrote:
> I have been trying to use some inventive alternative idioms for infinite
> loops in my code, rather than the same old for(;;) and while(1) - this
> could be a nice amusing Easter-egg for any future maintenance
> programmers!
May I suggest a different kind of solution for your problem?
Ok, then I suggest that you _not_ be a pain in the arse for your
successor.
Richard
| |
| Spiros Bousbouras 2007-12-19, 6:58 pm |
| On Dec 19, 12:40 pm, Bob <n...@spam.com> wrote:
> Hi,
>
> I have been trying to use some inventive alternative idioms for infinite
> loops in my code, rather than the same old for(;;) and while(1) - this
> could be a nice amusing Easter-egg for any future maintenance
> programmers!
>
> One of my ideas was this:
>
> unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
> while(++u>=0)
> {
> // loop
>
> }
>
> Unfortunately, the compiler gives a warning for this, which rather
> spoils the fun... does the compiler have to give a diagnostic here...
No , the compiler doesn't have to give a warning but
I would expect from any decent diagnostic tool (which
includes a compiler with all warnings on) to warn you
that the while test is always true.
| |
| Keith Thompson 2007-12-19, 6:58 pm |
| Bob <no@spam.com> writes:
> I have been trying to use some inventive alternative idioms for infinite
> loops in my code, rather than the same old for(;;) and while(1) - this
> could be a nice amusing Easter-egg for any future maintenance
> programmers!
>
> One of my ideas was this:
>
> unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
> while(++u>=0)
> {
> // loop
> }
If you do this kind of thing in production code, I hope I never have
to work with you.
> Unfortunately, the compiler gives a warning for this, which rather
> spoils the fun...
[...]
Good for the compiler. You should pay attention to the warning and
write clear code -- "for thy creativity is better used in solving
problems than in creating beautiful new impediments to understanding"
(Henry Spencer, "The Ten Commandments for C Programmers").
--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
| |
|
| On 19 Dec 2007 at 13:04, pete wrote:
> Bob wrote:
>
> unsigned u;
>
> for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)
> {
> // loop
> }
Cool! That's a really neat solution, thanks.
The life of a maintenance programmer is a pretty boring one, and I think
finding the odd little amusement must make the task more fun!
| |
| Ian Collins 2007-12-19, 9:58 pm |
| James Kuyper wrote:
> Richard Heathfield wrote:
>
> But still capable of triggering a compiler warning from a sufficiently
> clever compiler.
How about :
for( u = 1; u&1; u += 2 )
--
Ian Collins.
| |
| Walter Roberson 2007-12-20, 3:58 am |
| In article <5su3b7F1ap6faU3@mid.individual.net>,
Ian Collins <ian-news@hotmail.com> wrote:
>James Kuyper wrote:
[color=darkred]
>How about :
>for( u = 1; u&1; u += 2 )
Theoretically, UINT_MAX could be even, which would lead to a wrapping
to 0 at the end of the first pass of the loop. I'm not certain
it would be allowed with C99, but C89 has fewer restrictions on
value representation and could (I believe) have a non-binary
value system.
Hmmm... let's see... unsigned char is guaranteed not to have any
trap value, but I don't recall that the same applies to unsigned
int, so (for example) UINT_MAX could be 2^N-2 and 2^N-1 could be
a trap value. Might be a bit of a nuisance to convert unsigned
long to unsigned int if it were that way: UINT_MAX+1 is repeatedly
subtracted, which in the nice binary case corresponds to just dropping
leading bits, but if UINT_MAX is 2^N-2 then you effectively
end up subtracting the higher order bytes from the lower order bytes
to make the wrapping semantics come out right. Something for the DS9001
perhaps.
--
We regret to announce that sub-millibarn resolution bio-hyperdimensional
plasmatic space polyimaging has been delayed until the release
of Windows Vista SP2.
| |
| Richard Heathfield 2007-12-20, 3:58 am |
| Walter Roberson said:
> In article <5su3b7F1ap6faU3@mid.individual.net>,
> Ian Collins <ian-news@hotmail.com> wrote:
>
>
>
>
> Theoretically, UINT_MAX could be even,
How?
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
| |
| Flash Gordon 2007-12-20, 3:58 am |
| Bob wrote, On 20/12/07 01:29:
> On 19 Dec 2007 at 13:04, pete wrote:
>
> Cool! That's a really neat solution, thanks.
>
> The life of a maintenance programmer is a pretty boring one, and I think
> finding the odd little amusement must make the task more fun!
Having done maintenance work I can tell you that I would take the effort
to work back through the changes to find out who put that in and then
recommend to the head of development that they get rid of the person
responsible and the people who let the code through review.
If you want to do it for an IOCC entry, fine, if you want to do it for
your own amusement, fine. If you really are doing it to make the life of
a maintenance programmer "more fun" then you should not be working on
software at all, not as a job and not as a contributor to open source
projects.
--
Flash Gordon
| |
| Ben Bacarisse 2007-12-20, 7:57 am |
| roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
> Theoretically, UINT_MAX could be even,
I don't think so.
> which would lead to a wrapping
> to 0 at the end of the first pass of the loop. I'm not certain
> it would be allowed with C99, but C89 has fewer restrictions on
> value representation and could (I believe) have a non-binary
> value system.
No, C89 says: "The representations of integral types shall define
values by use of a pure binary numeration system."
> Hmmm... let's see... unsigned char is guaranteed not to have any
> trap value, but I don't recall that the same applies to unsigned
> int, so (for example) UINT_MAX could be 2^N-2 and 2^N-1 could be
> a trap value.
UINT_MAX is defined to be the maximum value of an object of unsigned
int type. This type is made up of N value bits whose values are
consecutive powers of two from 2^0 upwards. I can see no permission
for some values to be excluded from UINT_MAX.
unsigned int can have trap representations, but a bit is either a
value bit or a non-value bit. If the "top" bit is a value bit, all
values where it is combined with settings of the other values bits are
legal and permitted. If it is a padding bit, then you have fewer
value bits (N is one less) but UINT_MAX is still 2^N - 1.
This is from C99 and there may be more wriggle room in C89, but if so,
I would guess it is unintended.
--
Ben.
| |
| CBFalconer 2007-12-20, 7:57 am |
| pete wrote:
>
.... snip ...
>
> unsigned u;
>
> for (u = 1 /* or any odd number */ ; u++ > 0u; ++u)
> {
> // loop
> }
More useful snippet:
puts("Enter answer:"); fflush(stdout);
for (u = 1; u++; ++u) continue;
:-)
--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com
| |
| James Kuyper 2007-12-20, 7:57 am |
| Ian Collins wrote:
> James Kuyper wrote:
>
> How about :
>
> for( u = 1; u&1; u += 2 )
A compiler is free to complain about anything it wants to. Any idiom for
a loop that never exits that is sufficiently clear to not count as
obfuscated is sufficiently simple that a sufficiently clever compiler
can recognize it. And a loop that never exits is a reasonable thing for
a compiler to warn about.
There's no guaranteed way to avoid such a message; the best you can do
is find a way that silences the messages that a particular compiler
generates, or learn to live with the warning messages.
| |
| James Kuyper 2007-12-20, 7:57 am |
| Bob wrote:
> On 19 Dec 2007 at 13:04, pete wrote:
>
> Cool! That's a really neat solution, thanks.
>
> The life of a maintenance programmer is a pretty boring one, and I think
> finding the odd little amusement must make the task more fun!
Your little amusement may end up causing problems when the next
maintenance programmer wastes time trying to figure out the purpose of
your loop control. Write clear, easy to understand code, and let the
warnings fall where they may.
| |
| James Kuyper 2007-12-20, 7:58 am |
| Walter Roberson wrote:
> In article <5su3b7F1ap6faU3@mid.individual.net>,
> Ian Collins <ian-news@hotmail.com> wrote:
>
>
>
>
> Theoretically, UINT_MAX could be even,
Section 6.2.6.2p1 of the C99 standard is quite clear about this; if N is
the number of value bits in an unsigned type, the corresponding maximum
is 2^N-1, which is inherently never a negative number.
There's no correspondingly specific description for signed integer types.
| |
| Richard Heathfield 2007-12-20, 7:58 am |
| James Kuyper said:
> Walter Roberson wrote:
>
> Section 6.2.6.2p1 of the C99 standard is quite clear about this; if N is
> the number of value bits in an unsigned type, the corresponding maximum
> is 2^N-1, which is inherently never a negative number.
ITYM "never an even number".
But of course, it is easy to show that this is wrong. If N is, say,
15.000044+ (i.e. if there are 15.000044+ value bits in the unsigned type),
then the maximum is 32768, which is an even number.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
| |
| James Kuyper 2007-12-20, 9:58 pm |
| Richard Heathfield wrote:
> James Kuyper said:
>
>
> ITYM "never an even number".
>
> But of course, it is easy to show that this is wrong. If N is, say,
> 15.000044+ (i.e. if there are 15.000044+ value bits in the unsigned type),
> then the maximum is 32768, which is an even number.
I suppose you're correct; the standard never specifically requires that
there be an integer number of value bits. :-)
| |
| Eric Sosman 2007-12-20, 9:58 pm |
| Richard Heathfield wrote:
> James Kuyper said:
>
>
> ITYM "never an even number".
>
> But of course, it is easy to show that this is wrong. If N is, say,
> 15.000044+ (i.e. if there are 15.000044+ value bits in the unsigned type),
> then the maximum is 32768, which is an even number.
The Standard requires a "pure binary representation," but
the representation you suggest can only be called "impure."
--
Eric Sosman
esosman@ieee-dot-org.invalid
| |
| santosh 2007-12-20, 9:58 pm |
| James Kuyper wrote:
> Richard Heathfield wrote:
>
> I suppose you're correct; the standard never specifically requires
> that there be an integer number of value bits. :-)
The standard never specifically requires C to be restricted to computer
programming alone. :-)
| |
| Chris Dollin 2007-12-20, 9:58 pm |
| Eric Sosman wrote:
> Richard Heathfield wrote:
>
> The Standard requires a "pure binary representation," but
> the representation you suggest can only be called "impure."
Rubbish. "Bloody stupid" would be a reasonable alternative.
--
NonJohnson Hedgehog
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/
| |
| CBFalconer 2007-12-20, 9:58 pm |
| Eric Sosman wrote:
>
.... snip ...
>
> The Standard requires a "pure binary representation," but
> the representation you suggest can only be called "impure."
Ahh, now we are getting to something of general interest.
--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com
| |
| Walter Roberson 2007-12-20, 9:58 pm |
| In article <wI6dnXeP6oQt5ffanZ2dnUVZ_t-hnZ2d@comcast.com>,
Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
> The Standard requires a "pure binary representation," but
>the representation you suggest can only be called "impure."
Is that the C99 Standard? Is there a corresponding C89 clause?
I've been reviewing C89 but I have not yet found in there any
requirement of binary representation, beyond that implied by the definition
of "bit" and "byte". I also have not found any requirement in C89
that the complete possible span of binary be usable as values.
If there is anything in C89 that would prevent UINT_MAX from being
2^N-2 with 2^N-1 reserved as a trap value, could someone point me
to the relevant C89 clauses?
--
"All is vanity." -- Ecclesiastes
| |
| Ben Pfaff 2007-12-20, 9:58 pm |
| roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
> In article <wI6dnXeP6oQt5ffanZ2dnUVZ_t-hnZ2d@comcast.com>,
> Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>
>
> Is that the C99 Standard? Is there a corresponding C89 clause?
From C89 6.1.2.5 "Types": "The representations of integral types
shall defines values by use of a pure binary numeration system."
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da
6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
| |
| Walter Roberson 2007-12-20, 9:58 pm |
| In article <878x3p6poi.fsf@blp.benpfaff.org>,
Ben Pfaff <blp@cs.stanford.edu> wrote:
>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
[color=darkred]
[color=darkred]
[color=darkred]
>From C89 6.1.2.5 "Types": "The representations of integral types
>shall defines values by use of a pure binary numeration system."
Thanks. It appears the C89 clause is 3.1.2.5; "6.1.2.5" must be the C90
numbering.
I do see the quoted sentance in C89 now, but even with all of the
discussions there about subranges and representations, it is not
yet clear to me that in C89, UINT_MAX could not be 2^N-2 with 2^N-1
reserved. (It would require more than 16 bits for int, but that's not
difficult at all these days.)
--
"Any sufficiently advanced bug is indistinguishable from a feature."
-- Rich Kulawiec
| |
| geoyar 2007-12-23, 10:07 am |
| quote: Originally posted by pete
Bob wrote:
>
> Hi,
>
> I have been trying to use some inventive alternative
> idioms for infinite
> loops in my code, rather than the same old for(;;) and while(1) - this
> could be a nice amusing Easter-egg for any future maintenance
> programmers!
>
> One of my ideas was this:
>
> unsigned u = SHRT_MAX << 2; // misleading initialization - tee hee!
> while(++u>=0)
> {
> // loop
> }
>
> Unfortunately, the compiler gives a warning for this, which rather
> spoils the fun...
> does the compiler have to give a diagnostic here,
If it did, I could look it up.
> and can anyone think of a way
> of using the same basic idea but without generating a warning?
How to suppress an unidentified warning?
That's not a bad one.
unsigned u = SHRT_MAX / 1000;
while(--u >= 0u)
{
// loop
}
--
pete
Why did you do it?
Look how it works:
1. Compiler increments u on the stack and moves it to an accumulator;
2. If the compiler can't increment u on the stack, it should load u from the stack into accumulator, and increase it.
3. Compiler compares u to 0u
4. Compiler issues an instruction to jump to the beginning of the loop
This is a lot of work.
So, why do you need this invention? Why do create a problem?
Of course the compiler might optimize all this stuff out altogether.
By a way, the best code for the infinite loop depends on the compiler.
For example, on IAR's C compiler the best code is for(;;). It consists of jump only, no comparison at all.
On the same compiler while(1) loads 1 into the accumulator, tests it against zero and jumps | |
| Army1987 2007-12-29, 6:59 pm |
| Ian Collins wrote:
[About infinite loops]
> How about :
>
> for( u = 1; u&1; u += 2 )
>
It's the reversed form of the Italian humorous threat "Ti prendo a
schiaffi a due a due finché non diventano dispari", "I'm giving you slaps
two by two until they become odd".
BTW, what about
#define HELL_TEMPER 1.416785e32 /* actually, Planck temperature */
#define FREEZING_TEMPER 273.15
#define HELL_FREEZES_OVER (HELL_TEMPER < FREEZING_TEMPER)
while (!HELL_FREEZES_OVER) { ... }
?
--
Army1987 (Replace "NOSPAM" with "email")
|
|
|
|
|