Discarded Function Values (To Cast or Not to Cast) 
Author Message
 Discarded Function Values (To Cast or Not to Cast)

I suspect this is not a novel inquiry, but I can't remember any
previous discussion. . . .

My compiler (pcc) is agnostic about discarding the value from a
function call.  Lint, of course, protests.

If it's my function, I presumably will declare it to be void if
it doesn't return a value and will use the value if it does return
one.  But what about a standard library function whose value I
(perhaps recklessly) wish to ignore?  Should I cast the function
call to void, or am I just indulging in the trivial luxury of
silencing lint?

--
Chris Kern                           Voice of America, Washington, D.C.
...uunet!voa3!ck                                        +1 202-485-7020



Mon, 19 Apr 1993 18:58:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)

Quote:
> ...  Should I cast the function
> call to void, or am I just indulging in the trivial luxury of
> silencing lint?

Although you are merely silencing lint, you are also doing yourself
a service.  If you start letting warnings like "return value ignored"
pop up then you will someday miss an real warning/error message that
is buried amongst the "fake" warning/error messages.  Silence as many
lint warnings as you can (will somebody PLEASE do something about the
malloc bugaboo) and the real ones will stand out easily.
--
Michael Sullivan          uunet!jarthur.uucp!aqdata!sullivan
aQdata, Inc.
San Dimas, CA


Mon, 19 Apr 1993 15:46:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)

Quote:

>But what about a standard library function whose value I
>(perhaps recklessly) wish to ignore?  Should I cast the function
>call to void, or am I just indulging in the trivial luxury of
>silencing lint?

C does not require the (void) cast of the unused return value.
As a matter of policy, you shouldn't do things JUST to "shut lint up".
Lint is warning you that functions return success status etc. by
design and that it may be unwise to fail to check the returned status.
Therefore, I would say that for each such warning you should THINK
whether or not the returned value is important for the context in
which the function is invoked, and if so do something about it, or
if not then explicitly discard the value with a (void) cast.  If you
follow that policy, then the reader of your code will KNOW that (void)
means that you've thought about it and decided the return value was
not needed.  Without the explicit (void) he would have no way to tell
if there had been an oversight.


Mon, 19 Apr 1993 15:47:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)
[ My compiler (pcc) is agnostic about discarding the value from a
[ function call.  Lint, of course, protests.
[ If it's my function, I presumably will declare it to be void if
[ it doesn't return a value and will use the value if it does return
[ one.  But what about a standard library function whose value I
[ (perhaps recklessly) wish to ignore?  Should I cast the function
[ call to void, or am I just indulging in the trivial luxury of
[ silencing lint?

We've found that lint catches programmer bugs often enough to make it
worthwhile to do the casts of standard library functions, so much so
that we require all programs to 'pass' lint before they can be released.
Lint can point out bugs that would take a programmer hours to find,
so we believe there's no excuse _not_ to use lint.
--


(602) 861-7609



Mon, 19 Apr 1993 22:49:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)


: > ...  Should I cast the function
: > call to void, or am I just indulging in the trivial luxury of
: > silencing lint?
:
: Although you are merely silencing lint, you are also doing yourself
: a service.  If you start letting warnings like "return value ignored"
: pop up then you will someday miss an real warning/error message that
: is buried amongst the "fake" warning/error messages.  Silence as many
: lint warnings as you can (will somebody PLEASE do something about the
: malloc bugaboo) and the real ones will stand out easily.

I don't worry about lint warnings about functions whose return
value is never used. It is generally a very short list, things
like strcpy and the like, where the return value is never of
interest to me. I use a cast to void when I ignore the return
value of a function whose return value is used elsewhere.

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill



Mon, 19 Apr 1993 06:23:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)

Quote:
>I don't worry about lint warnings about functions whose return
>value is never used. It is generally a very short list, ...

If it isn't a problem for you, more power to you.
I find that I am less likely to overlook genuine problems
reported by "lint" if NO lint output is expected than if
SOME lint output is expected.  This is difficult to enforce
when malloc() is involved, although there are ways.


Mon, 19 Apr 1993 00:57:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)

: >I don't worry about lint warnings about functions whose return
: >value is never used. It is generally a very short list, ...
:
: If it isn't a problem for you, more power to you.

Not so long as I keep the list *short*.

: I find that I am less likely to overlook genuine problems
: reported by "lint" if NO lint output is expected than if
: SOME lint output is expected.  This is difficult to enforce
: when malloc() is involved, although there are ways.

I've found that completely shutting up lint is not worth my time.
Not that it is impossible, but I have better things to do.

For example, read() and write(), on different machines, take
either an unsigned int or an int as their third arguments; while
one *could* set things up so that lint won't complain about this,
why bother?

Another gotcha is those irritating truncated long messages; at
least one lint I've worked with won't stop them even if you use a
cast. (And what is so irritating is that one *can't* ignore them.)

Another irritation is the sizeof problem. Things that want a size
should take an unsigned something (or a size_t). But sizeof often
generates an int, which some lints complains about.

Anyway, my approach to this is to shut lint up on anything where
I might be confused by its output, and ignore the rest. (In one
case, I got sufficiently irritated that I wrote a script to
remove the noise. This is risky, unfortunately.)

One thing that helps is that I do most of my work development at
home on my sysV/386 and immediately port it to the Sun at work.
(Typically, I develop it here at home and then run it there:
4MIPS bests ~2MIPS, especially when it takes two hours even at 4!)
Porting is usually a copy, but I sometime catch errors that way.
The real advantage is that, when coding, I am pretty much required
to keep in mind that there are at *least* three other kinds of
machines that my code *will* be run on.

That is a real incentive to avoid those practices that lint would
otherwise complain about.

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill



Mon, 19 Apr 1993 17:18:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)

Quote:


>>I don't worry about lint warnings about functions whose return
>>value is never used. It is generally a very short list, ...

>If it isn't a problem for you, more power to you.
>I find that I am less likely to overlook genuine problems
>reported by "lint" if NO lint output is expected than if
>SOME lint output is expected.  This is difficult to enforce
>when malloc() is involved, although there are ways.

I always make lint produce silent output, even when writing device
drivers.  Even if you can remember which lint messages are harmless,
someone inheriting your code has to examine them all, or ignore them
all.  And a few months later, if lint produces 7 messages instead of
6, I do not feel all that sure that 7th would catch my attention.  One
instead of zero, I will notice.
--

Jeffrey Kegler, Independent UNIX Consultant, Algorists, Inc.

1762 Wainwright DR, Reston VA 22090



Mon, 19 Apr 1993 12:13:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)

Quote:
>I've found that completely shutting up lint is not worth my time.

Agreed, but for the sake of curiosity...

Quote:
>Not that it is impossible, but I have better things to do.

What DO you do about calls to malloc?

--

  Should the preceding opinions be caught or     |  James Bond asked his
  killed, the sender will disavow all knowledge  |  ATT rep for a source
  of their activities or whereabouts.            |  licence to "kill".



Mon, 19 Apr 1993 10:04:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)
The only way I've found to shut lint up about malloc calls is:

        lint | grep -v 'possible pointer alignment problem'

Does anyone have a better way?  Please share it with us so we can stop
talking about this particular bugaboo.

                                        Lloyd Kremer

                                        Have terminal...will hack!



Mon, 19 Apr 1993 17:44:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)

Quote:

>What DO you do about calls to malloc?

1) Put something like this in /usr/lib/lint/llib-lc and regenerate llib-lc.ln:

    void  lint_malloc(n) unsigned n; { n = n; }

2) Put something like this in /usr/include/malloc.h (if you have one):

    #ifdef lint
    #   define  malloc(x)       (lint_malloc(x), 0)
    #else
        extern char             *malloc();
    #endif

-----



Mon, 19 Apr 1993 18:01:00 GMT  
 Discarded Function Values (To Cast or Not to Cast)
Wouldn't this be simpler?

Quote:
>     #ifdef lint

!     #   define  malloc(x)       (x, 0)
Quote:
>     #else
>         extern char             *malloc();
>     #endif

--

 'U`  --------------  +1 713 274 5180.
"I agree 0bNNNNN would have been nice, however, and I sure wish X3J11 had taken



Mon, 19 Apr 1993 23:04:00 GMT  
 
 [ 24 post ]  Go to page: [1] [2]

 Relevant Pages 

1. malloc: to cast or not to cast

2. to cast or not to cast?

3. When is a cast not a cast?

4. Need help with reinterpret_cast, a C-style cast or function-style cast

5. Need help with reinterpret_cast, a C-style cast or function-style cast

6. Need help with reinterpret_cast, a C-style cast or function-style cast

7. Need help with reinterpret_cast, a C-style cast or function-style cast

8. old style casts vs new style casts

9. casting of struct element void pointer casting

10. Cast...always cast...

11. old style casts vs new style casts

12. CString cast to LPCTSTR cast error

 

 
Powered by phpBB® Forum Software