Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   [Slightly OT] Casting non-void function results to void in modern compilers. (http://www.velocityreviews.com/forums/t316809-slightly-ot-casting-non-void-function-results-to-void-in-modern-compilers.html)

David M. Wilson 01-06-2004 08:58 AM

[Slightly OT] Casting non-void function results to void in modern compilers.
 
I know this doesn't specifically pertain to the scope of comp.lang.c,
but I don't know of a better forum to post it on, besides, what is the
practicality of comp.lang.c if it weren't for answers to questions
such as this? :)

I see an awful lot of code on a day to day basis like so:

(void) printf(...);


I was told in my fledgeling years that this was used as a hint to the
compiler to tell it not to save the return value of the called
function, because it isn't used. I always wondered about that,
considering the statement is not an assignment expression, and the
return value 'flows off the end of the expression' anyway.

Is this a useful modern optimisation? I would have thought not, but I
still see it in modern code. Surely a modern (or even ancient)
compiler can see that the result of printf() is not used, and so it
should perform any possible optimisations based on the fact the return
value isn't used.

Am I wrong? Thanks,


David.

Ed Morton 01-06-2004 09:05 AM

Re: [Slightly OT] Casting non-void function results to void in moderncompilers.
 


David M. Wilson wrote:

> I know this doesn't specifically pertain to the scope of comp.lang.c,
> but I don't know of a better forum to post it on, besides, what is the
> practicality of comp.lang.c if it weren't for answers to questions
> such as this? :)
>
> I see an awful lot of code on a day to day basis like so:
>
> (void) printf(...);
>
>
> I was told in my fledgeling years that this was used as a hint to the
> compiler to tell it not to save the return value of the called
> function, because it isn't used. I always wondered about that,
> considering the statement is not an assignment expression, and the
> return value 'flows off the end of the expression' anyway.
>
> Is this a useful modern optimisation? I would have thought not, but I
> still see it in modern code. Surely a modern (or even ancient)
> compiler can see that the result of printf() is not used, and so it
> should perform any possible optimisations based on the fact the return
> value isn't used.
>
> Am I wrong? Thanks,


The compiler can see that the return code isn't used, but it can't tell
whether or not it SHOULD have been used. By the author specifying the
"void" cast, they're telling the compiler (or lint) not to issue a
warning for that line as the return code is, in this case, being
intentionally discarded.

Ed.

>
> David.



Kevin Goodsell 01-06-2004 09:26 AM

Re: [Slightly OT] Casting non-void function results to void in moderncompilers.
 
David M. Wilson wrote:

> I know this doesn't specifically pertain to the scope of comp.lang.c,
> but I don't know of a better forum to post it on, besides, what is the
> practicality of comp.lang.c if it weren't for answers to questions
> such as this? :)
>
> I see an awful lot of code on a day to day basis like so:
>
> (void) printf(...);
>
>
> I was told in my fledgeling years that this was used as a hint to the
> compiler to tell it not to save the return value of the called
> function, because it isn't used.


I don't believe it accomplishes anything like that. The compiler
probably can't do anything useful with the information that the return
value is unused - the code for printf() (which probably resides in some
library somewhere that the compiler doesn't know or care about) is
already returning a value. printf isn't going to be re-written to omit
the return just because you cast it to void.

To the best of my knowledge, the cast-to-void technique is used solely
to silence warnings from static code checking tools like LINT, or from
over-zealous compilers.

> I always wondered about that,
> considering the statement is not an assignment expression, and the
> return value 'flows off the end of the expression' anyway.
>
> Is this a useful modern optimisation?


I don't believe it is, or has ever been, an optimization, useful or
otherwise.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

CBFalconer 01-06-2004 10:09 AM

Re: [Slightly OT] Casting non-void function results to void in moderncompilers.
 
"David M. Wilson" wrote:
>

.... snip ...
>
> I see an awful lot of code on a day to day basis like so:
>
> (void) printf(...);
>
> I was told in my fledgeling years that this was used as a hint to
> the compiler to tell it not to save the return value of the called
> function, because it isn't used. I always wondered about that,
> considering the statement is not an assignment expression, and the
> return value 'flows off the end of the expression' anyway.


Opinions vary, but one attitude is that:

a) This avoids generating 'result discarded' messages in lint.
b) This indicates to any future maintainer that there IS a
result being returned and discarded, which he may use if the
occasion arises.

Another attitude is that it is meaningless clutter.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!



Arthur J. O'Dwyer 01-06-2004 05:18 PM

Re: [Slightly OT] Casting non-void function results to void in modern compilers.
 

On Tue, 6 Jan 2004, David M. Wilson wrote:
>
> I know this doesn't specifically pertain to the scope of comp.lang.c,
> but I don't know of a better forum to post it on, besides, what is the
> practicality of comp.lang.c if it weren't for answers to questions
> such as this? :)
>
> I see an awful lot of code on a day to day basis like so:
>
> (void) printf(...);


Hee hee!

Message-ID: <Pine.LNX.4.58-035.0401051618400.6508@unix41.andrew.cmu.edu>
<quote>

> >>Ben.c:17:7: Return value (type int) ignored: putchar(p[i])
> >> Result returned by function call is not used.

> >
> > Gosh. Lint really _is_ prehistoric, isn't it?

>
> According to K&R2, putchar() does indeed return an int. I suspect
> a cast to void would silence lint, but it's hardly worth it, is it?


Some old code, and some overly-portable code, does use casts to
void. We even get questions about (void)printf here occasionally.

</quote>

-Arthur

Christian Bau 01-06-2004 08:38 PM

Re: [Slightly OT] Casting non-void function results to void in modern compilers.
 
In article <99dce321.0401060058.d7d4b0c@posting.google.com> ,
dw-google.com@botanicus.net (David M. Wilson) wrote:

> I know this doesn't specifically pertain to the scope of comp.lang.c,
> but I don't know of a better forum to post it on, besides, what is the
> practicality of comp.lang.c if it weren't for answers to questions
> such as this? :)
>
> I see an awful lot of code on a day to day basis like so:
>
> (void) printf(...);
>
>
> I was told in my fledgeling years that this was used as a hint to the
> compiler to tell it not to save the return value of the called
> function, because it isn't used. I always wondered about that,
> considering the statement is not an assignment expression, and the
> return value 'flows off the end of the expression' anyway.
>
> Is this a useful modern optimisation? I would have thought not, but I
> still see it in modern code. Surely a modern (or even ancient)
> compiler can see that the result of printf() is not used, and so it
> should perform any possible optimisations based on the fact the return
> value isn't used.


If a function returns a value, and the caller doesn't use that return
value, then there are two possibilities:

1. The caller made a mistake and should have used the value.
2. The caller knew what he/she was doing and had no need for the
value.

When you cast the return value to void, you explicitely tell the
compiler that yes, you know that the function returns a value, and no,
you don't want to use it. Without that cast, many compilers will give a
warning telling you that you might have missed something.

It is also a good hint to other programmers.

Peter Nilsson 01-07-2004 12:16 AM

Re: [Slightly OT] Casting non-void function results to void in modern compilers.
 
"Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> wrote in message news:<Pine.LNX.4.58-035.0401061216050.3684@unix45.andrew.cmu.edu>...
> On Tue, 6 Jan 2004, David M. Wilson wrote:
> >
> > I know this doesn't specifically pertain to the scope of comp.lang.c,
> > but I don't know of a better forum to post it on, besides, what is the
> > practicality of comp.lang.c if it weren't for answers to questions
> > such as this? :)
> >
> > I see an awful lot of code on a day to day basis like so:
> >
> > (void) printf(...);

>
> Hee hee!
>
> Message-ID: <Pine.LNX.4.58-035.0401051618400.6508@unix41.andrew.cmu.edu>
> <quote>
>
> > >>Ben.c:17:7: Return value (type int) ignored: putchar(p[i])
> > >> Result returned by function call is not used.
> > >
> > > Gosh. Lint really _is_ prehistoric, isn't it?

> >
> > According to K&R2, putchar() does indeed return an int. I suspect
> > a cast to void would silence lint, but it's hardly worth it, is it?

>
> Some old code, and some overly-portable code, does use casts to
> void.


To my knowledge, I've never read anything to suggest it was being done
for portability reasons. Is there any evidence of such?

I believe the standard inherited (and it remains in C99) the K&R C
behaviour of allowing functions which don't have an explicit return
statement to function as normal, so long as the calling function
didn't attempt to use a value from that function. So, C has always had
issues with ignored function return values.

I can understand why lint (et al) would give the warning, e.g...

sin(x);

But, I can't see that actual programming instances where the warning
might actually highlight an error would be terribly common.

I'm all ears (well eyes), if anyone has any anecdotes.

> We even get questions about (void)printf here occasionally.
>
> </quote>


--
Peter

Vijay Kumar R Zanvar 01-07-2004 07:10 AM

Re: [OT] Casting non-void function results to void in modern compilers.
 
[..]
> I can understand why lint (et al) would give the warning, e.g...


What does "et al" mean?

--
Vijay Kumar R Zanvar



Ben Pfaff 01-07-2004 07:32 AM

Re: [OT] Casting non-void function results to void in modern compilers.
 
"Vijay Kumar R Zanvar" <vijoeyz@hotpop.com> writes:

> [..]
> > I can understand why lint (et al) would give the warning, e.g...

>
> What does "et al" mean?


"and others"
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_


All times are GMT. The time now is 12:15 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.