Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Wrong-but-not-incorrect code

Reply
Thread Tools

Wrong-but-not-incorrect code

 
 
Dave Vandervies
Guest
Posts: n/a
 
      03-17-2005
Seen in a chunk of code I was looking at recently (not mine!):
--------
char* imgfilename[100];
sprintf((char*)imgfilename, "mask%d.dib", params.profile);
ReadImage((char*)imgfilename);
--------
(ReadImage is another part of the program's code that does exactly what
the reasonable reader would expect.)

For the CLC readers:
Can you, by artifical construction or actual experience, come up with
something that's more Wrong and yet still manages to be correct code
that performs the intended task?


dave

--
Dave Vandervies http://www.velocityreviews.com/forums/(E-Mail Removed)
It is true that their choices are not always consonant with the choices _I_
would make, but that's because they're cats and I'm not.
--Mike Andrews in the scary devil monastery
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-17-2005


Dave Vandervies wrote:
> Seen in a chunk of code I was looking at recently (not mine!):
> --------
> char* imgfilename[100];
> sprintf((char*)imgfilename, "mask%d.dib", params.profile);
> ReadImage((char*)imgfilename);
> --------
> (ReadImage is another part of the program's code that does exactly what
> the reasonable reader would expect.)
>
> For the CLC readers:
> Can you, by artifical construction or actual experience, come up with
> something that's more Wrong and yet still manages to be correct code
> that performs the intended task?


My personal favorite is this one-liner a colleague
found many years ago:

#define HASHSIZE 51 /* a small prime */

--
(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      03-17-2005
>
> My personal favorite is this one-liner a colleague
> found many years ago:
>
> #define HASHSIZE 51 /* a small prime */
>
> --
> (E-Mail Removed)


He obiously doesnt know his 3 time table above 3x10
Allan
>



 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-17-2005
Dave Vandervies wrote:

> Seen in a chunk of code I was looking at recently (not mine!):
> --------
> char* imgfilename[100];
> sprintf((char*)imgfilename, "mask%d.dib", params.profile);
> ReadImage((char*)imgfilename);
> --------
> (ReadImage is another part of the program's code that does exactly what
> the reasonable reader would expect.)
>
> For the CLC readers:
> Can you, by artifical construction or actual experience, come up with
> something that's more Wrong and yet still manages to be correct code
> that performs the intended task?
> ...


signed char direction; /* either -1 or +1 */
unsigned offset; /* absolute distance */
T* ptr;

...
ptr += direction * offset;
/* Moving the pointer in direction specified by 'direction' */

In general case the code does not do what it was intended to do, since
the right hand side will be promoted and evaluated within the bounds of
'unsigned' type. However, on a 32 bit platform the code "worked" - the
pointer value wrapped around and ended up exactly where it would be if
code worked as intended. On a 64 bit platform (32-bit 'unsigned' and
64-bit pointers) the code started to fail for obvious reasons.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-17-2005
Dave Vandervies wrote:
> Seen in a chunk of code I was looking at recently (not mine!):
> --------
> char* imgfilename[100];
> sprintf((char*)imgfilename, "mask%d.dib", params.profile);
> ReadImage((char*)imgfilename);
> --------
> (ReadImage is another part of the program's code that does exactly what
> the reasonable reader would expect.)
>
> For the CLC readers:
> Can you, by artifical construction or actual experience, come up with
> something that's more Wrong and yet still manages to be correct code
> that performs the intended task?


Your example itself could easily be made Worse. That sprintf call, for
example, is missing some casts, innit? And both that and the ReadImage
call could used a (void), innit?
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      03-17-2005
In article <d1cv00$huf$(E-Mail Removed)>
Dave Vandervies <(E-Mail Removed)> wrote:
>Can you, by artifical construction or actual experience, come up with
>something that's more Wrong and yet still manages to be correct code
>that performs the intended task?


I have no idea how to rate the "wrongness" on a scale that
affords comparisons, but I once put a typo in my code so
that instead of:

if (expr && var)

I had:

if (expr &*& var)

The code still worked because the expression (whatever it was)
produced either 0 or 1 as its result -- it was perhaps something
like "a == b" -- and "var" was also logically boolean, holding
only either 0 or 1. Hence:

if (a == b &*& c)

was:

if ((a == b) & (*&c))

which was:

if ((a == b) & c)

which meant the same thing as the intended line.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Kenneth Bull
Guest
Posts: n/a
 
      03-18-2005
Dave Vandervies wrote:
> Seen in a chunk of code I was looking at recently (not mine!):
> --------
> char* imgfilename[100];
> sprintf((char*)imgfilename, "mask%d.dib", params.profile);
> ReadImage((char*)imgfilename);
> --------
> (ReadImage is another part of the program's code that does exactly

what
> the reasonable reader would expect.)
>
> For the CLC readers:
> Can you, by artifical construction or actual experience, come up with
> something that's more Wrong and yet still manages to be correct code
> that performs the intended task?
>
>


This isn't technically wrong but I thought I'd mention it. I knew this
chap who really loved GOTO statements, but upon entering a job he
realized the company's coding practices didn't allow them. So instead
he dropped a bunch of JUMPahead and JUMPbehind statemetns in his code
as a replacement. In a header file he hid the following:

jumpcommands.h
===============

#define JUMPahead goto
#define JUMPbehind goto

-------------------------

Makes you wonder why I.Q. tests are given to job applicants anymore.
BTW, this chap had a lot of fancy -qualifications-. If you ask me, the
onyl qualification that matters is the one between someone's ears.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-18-2005
Eric Sosman wrote:
>

.... snip ...
>
> My personal favorite is this one-liner a colleague
> found many years ago:
>
> #define HASHSIZE 51 /* a small prime */


A few years ago, in this very newsgroup, 91 enjoyed similar fame.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
Charles Richmond
Guest
Posts: n/a
 
      03-18-2005
Eric Sosman wrote:
>
> Dave Vandervies wrote:
> > Seen in a chunk of code I was looking at recently (not mine!):
> > --------
> > char* imgfilename[100];
> > sprintf((char*)imgfilename, "mask%d.dib", params.profile);
> > ReadImage((char*)imgfilename);
> > --------
> > (ReadImage is another part of the program's code that does exactly what
> > the reasonable reader would expect.)
> >
> > For the CLC readers:
> > Can you, by artifical construction or actual experience, come up with
> > something that's more Wrong and yet still manages to be correct code
> > that performs the intended task?

>
> My personal favorite is this one-liner a colleague
> found many years ago:
>
> #define HASHSIZE 51 /* a small prime */
>

"If 91 were prime, it would be a counterexample to your conjecture."
-- Bruce Wheeler

--
+----------------------------------------------------------------+
| Charles and Francis Richmond It is moral cowardice to leave |
| undone what one perceives right |
| richmond at plano dot net to do. -- Confucius |
+----------------------------------------------------------------+
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      03-18-2005
Allan Bruce wrote:
>
> >
> > My personal favorite is this one-liner a colleague
> > found many years ago:
> >
> > #define HASHSIZE 51 /* a small prime */
> >
> > --
> > (E-Mail Removed)

>
> He obiously doesnt know his 3 time table above 3x10
> Allan


I add up the digits to see if a number is divisible by 3.
(5 + 1) % 3 == 0

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM
Re: Code Behind vs. no code behind: error Ben Miller [msft] ASP .Net 1 06-28-2003 01:46 AM
Re: C# Equivalent of VB.Net Code -- One line of code, simple Ian ASP .Net 0 06-25-2003 01:14 PM
Re: C# Equivalent of VB.Net Code -- One line of code, simple Ron ASP .Net 1 06-24-2003 07:18 PM



Advertisments