Article View: comp.lang.c.moderated
Article #2140Re: Casting to (void)
From: rpp
Date: Fri, 01 Mar 1996 15:43
Date: Fri, 01 Mar 1996 15:43
38 lines
2239 bytes
2239 bytes
In article: <4gnb5s$bed@solutions.solon.com> "Thomas A. Stolz" <stolz@mtm.syr.ge.com>
writes:
>
> Several of my coworkers have recently brought to my attention published code
> which always casts unused return values to (void). For example, this code
> would use the statement:
>
> (void)strcpy( dest, src );
>
> rather than the statement:
>
> strcpy( dest, src );
>
> I see no advantage in using the cast. In fact, I would tend to believe it
> would make the code harder to read, especially for inexperienced programmers.
> But, I'm curious and would like to know the pros and cons of each form. I
> would appreciate any information which discusses this topic.
>
> [lint has a warning: foo returns a value which is {sometimes,always}
> ignored. The cast is to shut it up by making it explicit that one
> is calling for side effects, and that testing for failure does not
> apply. There may be more to it than that, though. -mod]
>
> Thanks!
> Tom
>
>
The reason for doing this is maintainability. Anyone subsequently
trying to understand the code can see that the function was called for
its side-effects and the result *intentionally* thrown away. This
practice is often mandated in coding standards for safety-critical
applications. It also helps to force the coder to think before ignoring
the error returns from system calls.
--
Richard P. Parkins, M. A. rpp@pires.co.uk
Pi Technology Ltd. +44 (0)1223 441434
Opinions expressed were mine at the time of posting,
otherwise all possible disclaimers apply.
Technical Details
Message-ID:
<4h75ua$51c@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4gnb5s$bed@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: rpp <rpp@pires.co.uk> Newsgroups: comp.lang.c.moderated Subject: Re: Casting to (void) Date: 1 Mar 1996 09:43:06 -0600 Organization: Pi Technology Ltd Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4h75ua$51c@solutions.solon.com> References: <4gnb5s$bed@solutions.solon.com> Reply-To: rpp@pires.co.uk NNTP-Posting-Host: solutions.solon.com X-NNTP-Posting-Host: pires.demon.co.uk X-Broken-Date: Thursday, Feb 29, 1996 15.51.22 GMT X-Newsreader: Newswin Alpha 0.6 X-Mail2News-Path: disperse.demon.co.uk!post.demon.co.uk!pires.demon.co.uk