473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

best way to suppress "unused" warning?

I'm using an API which does a lot of callbacks. In classic callback
style, each routine provides a void * pointer to carry user-defined
data. Sometimes, however, the user-defined pointer is not needed which
causes the compiler to spit out a "helpful" warning. For example:

% cat unused.c
#include <stdio.h>
int
foo(char *str, void *data)
{
puts(str);
return 0;
}

% gcc -c -W -Wall unused.c
unused.c:4: warning: unused parameter 'data'

I'm looking for an appropriate (safe, reasonably self-documenting) way
of suppressing that warning in cases where I expect it (thus I'm not
looking for the compiler flags to suppress the warning - I just want to
be able to mark the places I know about). I've used constructs like

data = data;
data++;
if (data != data) return;

And they all work but are a little too obfuscated for my taste. I'm sure
many other people have run into this - is there a recommended technique?

RM
Jun 27 '08 #1
13 9686
Rex Mottram said:
I'm using an API which does a lot of callbacks. In classic callback
style, each routine provides a void * pointer to carry user-defined
data. Sometimes, however, the user-defined pointer is not needed which
causes the compiler to spit out a "helpful" warning. For example:

% cat unused.c
#include <stdio.h>
int
foo(char *str, void *data)
{
puts(str);
return 0;
}

% gcc -c -W -Wall unused.c
unused.c:4: warning: unused parameter 'data'

I'm looking for an appropriate (safe, reasonably self-documenting) way
of suppressing that warning in cases where I expect it (thus I'm not
looking for the compiler flags to suppress the warning - I just want to
be able to mark the places I know about). I've used constructs like

data = data;
data++;
if (data != data) return;

And they all work but are a little too obfuscated for my taste. I'm sure
many other people have run into this - is there a recommended technique?
Lots of people use (void)data for this, and it's just as good a way as any
of those you mention, although I'd hesitate before claiming it's
"recommended" because such a claim leaves some important questions
unanswered (for instance, "recommended by whom?").

You might want to consider:

#define DISCARD_PARAMETER (void)

Usage:

DISCARD_PARAMETER data;

or you might prefer:

#define DISCARD_PARAMETER(p) (void)p

Usage:

DISCARD_PARAMETER(data);

--
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
Jun 27 '08 #2
On 25 Apr 2008 at 1:24, Richard Heathfield wrote:
Lots of people use (void)data for this, and it's just as good a way as
any of those you mention
Obfuscating your code to pacify your compiler or lint smacks to me of
letting the tail wag the dog.

Jun 27 '08 #3
Antoninus Twink wrote:
On 25 Apr 2008 at 1:24, Richard Heathfield wrote:
>Lots of people use (void)data for this, and it's just as good a way
as any of those you mention

Obfuscating your code to pacify your compiler or lint smacks to me of
letting the tail wag the dog.
In real life there may be coding standards that demand for this.

At one company I used to work for it was mandatory to use "-Wall -Werror",
or something eqivalent and all new code and on all older code once it got
touched.

It was a pain in the proverbian, but lead to much better code and lots of
yet unexplained faults mysteriously disappaered...

Bye, Jojo
Jun 27 '08 #4
In article <sl*******************@nospam.invalid>,
Antoninus Twink <no****@nospam.invalidwrote:
>Lots of people use (void)data for this, and it's just as good a way as
any of those you mention
>Obfuscating your code to pacify your compiler or lint smacks to me of
letting the tail wag the dog.
It's a difficult balance to strike. I have at least once missed an
important warning because it was surrounded by unimportant ones that
I was expecting.

-- Richard

--
:wq
Jun 27 '08 #5
On Apr 24, 6:05*pm, Rex Mottram <r...@not.herewrote:
I'm using an API which does a lot of callbacks. In classic callback
style, each routine provides a void * pointer to carry user-defined
data. Sometimes, however, the user-defined pointer is not needed which
causes the compiler to spit out a "helpful" warning. For example:

% cat unused.c
#include <stdio.h>
int
foo(char *str, void *data)
{
* * *puts(str);
* * *return 0;

}

% gcc -c -W -Wall unused.c
unused.c:4: warning: unused parameter 'data'

I'm looking for an appropriate (safe, reasonably self-documenting) way
of suppressing that warning in cases where I expect it (thus I'm not
looking for the compiler flags to suppress the warning - I just want to
be able to mark the places I know about). I've used constructs like

* * * * data = data;
* * * * data++;
* * * * if (data != data) return;

And they all work but are a little too obfuscated for my taste. I'm sure
many other people have run into this - is there a recommended technique?
This is non-standard, but many compilers will not issue such
a warning when the comment /* ARGSUSED *? immediately
precedes the function:

/* ARGSUSED */
foo(char *str, void *data)
{
...
}

--
Fred Kleinschmidt

Jun 27 '08 #6
Richard Tobin wrote:
Antoninus Twink <no****@nospam.invalidwrote:
.... snip ...
>
>Obfuscating your code to pacify your compiler or lint smacks to
me of letting the tail wag the dog.

It's a difficult balance to strike. I have at least once missed
an important warning because it was surrounded by unimportant
ones that I was expecting.
Which, to me, points out the importance of handling those
'unimportant' warnings. I think there are very few which cannot be
eliminated by minor rewriting.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #7
On 24 Apr 2008, Rex Mottram <re**@not.herewrote:
I'm using an API which does a lot of callbacks. In classic
callback style, each routine provides a void * pointer to carry
user-defined data. Sometimes, however, the user-defined pointer is
not needed which causes the compiler to spit out a "helpful"
warning. For example:

% cat unused.c
#include <stdio.h>
int
foo(char *str, void *data)
foo(char *str, void *unused)
{
puts(str);
return 0;
}

% gcc -c -W -Wall unused.c
unused.c:4: warning: unused parameter 'data'
unused.c:4: warning: unused parameter 'unused'

OK, it doesn't *suppress* the warning. But it does make it
*reasonably* self-documenting ;-)

Dave

--
D.a.v.i.d T.i.k.t.i.n
t.i.k.t.i.n [at] a.d.v.a.n.c.e.d.r.e.l.a.y [dot] c.o.m
Jun 27 '08 #8
Joachim Schmitz wrote:
Antoninus Twink wrote:
Richard Heathfield wrote:
Lots of people use (void)data for this, and it's just as good
a way as any of those you mention
Obfuscating your code to pacify your compiler or lint smacks
to me of letting the tail wag the dog.

In real life there may be coding standards that demand for this.
Of course, tail wagging the dog is standard policy in many
avenues of life. That doesn't mean it's sensible.
At one company I used to work for it was mandatory to use
"-Wall -Werror", or something eqivalent and all new code and
on all older code once it got touched.
So if the compiler ever get's upgraded, you may have to 'correct'
thousands of lines of perfectly correct code because the compiler
writers decided to add another warning? Brilliant.

Did you have to rewrite third party library sources as well?
It was a pain in the proverbian, but lead to much better code
Warning free is not a measure of 'better'. But I'm in a minority
on that.
and lots of yet unexplained faults mysteriously disappaered...
I'm sure it also lead to the appearance (albeit rare) of
mysterious faults that went undiscovered for some time until a
critical moment precisely because all the programmers have
been conditioned to write code that eludes diagnostics and
static analysis.

Ever heard the term superbugs. ;-)

--
Peter
Jun 27 '08 #9
Peter Nilsson said:

<snip>
Warning free is not a measure of 'better'. But I'm in a minority
on that.
But not a minority of one. I happen to agree with you on this.
Nevertheless, *to a first approximation*, a low warning count is better
than a high warning count. If code generates a vast number of warnings,
that is typically not a good sign, although there might be a good reason
for it. (Think pulse rate - 160 heartbeats a minute is not generally
considered healthy, but if the measuree has just completed a run of 10,000
metres it probably counts as pretty low!)

<snip>

--
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
Jun 27 '08 #10
"Peter Nilsson" <ai***@acay.com.auschrieb im Newsbeitrag
news:bb**********************************@l28g2000 prd.googlegroups.com...
Joachim Schmitz wrote:
>Antoninus Twink wrote:
Richard Heathfield wrote:
Lots of people use (void)data for this, and it's just as good
a way as any of those you mention

Obfuscating your code to pacify your compiler or lint smacks
to me of letting the tail wag the dog.

In real life there may be coding standards that demand for this.

Of course, tail wagging the dog is standard policy in many
avenues of life. That doesn't mean it's sensible.
>At one company I used to work for it was mandatory to use
"-Wall -Werror", or something eqivalent and all new code and
on all older code once it got touched.

So if the compiler ever get's upgraded, you may have to 'correct'
thousands of lines of perfectly correct code because the compiler
writers decided to add another warning? Brilliant.
The vast majority was stuff like comment inside comment, steming from the
function headers like this:

/***********
/*
/* some text describing the function
*/

Once they've been fixed, the real warnings became visible.
Statement with no effect (e.g. from ; after while())
conversion of <typeto <typewithout a case (and no the fix was not to add
the cast)
implicit declaration of <function>
unused variable

etc.

Newer compiler versions didn't create many new warnings, so that wasn't much
of an issue,
but porting to a different platform (and compiler) then produced a new and
extensive set of warnings, the majority usefull and correct ones.
Did you have to rewrite third party library sources as well?
There were none, fortunatly.
>It was a pain in the proverbian, but lead to much better code

Warning free is not a measure of 'better'. But I'm in a minority
on that.
>and lots of yet unexplained faults mysteriously disappaered...
One example was lots of warnigs about unused variables. We removed these and
the code didn't crash anymore. Later we learned that that particular
compiler's optimizer had a problem with unused vairables, resulting in bad
code.
OK, in this case a compiler upgrade would have fixed it too, but at that
time we a) didn't know and b) the fixed compiler wasn't available.
I'm sure it also lead to the appearance (albeit rare) of
mysterious faults that went undiscovered for some time until a
critical moment precisely because all the programmers have
been conditioned to write code that eludes diagnostics and
static analysis.
I'm not aware of any

Bye, Jojo
Jun 27 '08 #11

"Peter Nilsson" <ai***@acay.com.auschrieb im Newsbeitrag
news:bb**********************************@l28g2000 prd.googlegroups.com...
Joachim Schmitz wrote:
<snip>
>(removing the warnings) was a pain in the proverbian, but lead to much
better code

Warning free is not a measure of 'better'. But I'm in a minority
on that.
It was not (only) better because of the lack of warnings, but (mainly) due
to the numbers of bugs we spotted and removed in the due course of
investigating and eliminating the warnings.

Bye, Jojo
Jun 27 '08 #12
Joachim Schmitz wrote:
Antoninus Twink wrote:
>On 25 Apr 2008 at 1:24, Richard Heathfield wrote:
>>Lots of people use (void)data for this, and it's just as good a way
as any of those you mention
Obfuscating your code to pacify your compiler or lint smacks to me of
letting the tail wag the dog.
In real life there may be coding standards that demand for this.
Adding unnamed parameters would fix this case and break nothing. But I
guess there little chance of such a simple change making its way into
the standard.

--
Ian Collins.
Jun 27 '08 #13
Rex Mottram wrote:
I'm using an API which does a lot of callbacks. In classic callback
style, each routine provides a void * pointer to carry user-defined
data. Sometimes, however, the user-defined pointer is not needed which
causes the compiler to spit out a "helpful" warning. For example:

% cat unused.c
#include <stdio.h>
int
foo(char *str, void *data)
{
puts(str);
return 0;
}

% gcc -c -W -Wall unused.c
unused.c:4: warning: unused parameter 'data'

I'm looking for an appropriate (safe, reasonably self-documenting) way
of suppressing that warning in cases where I expect it (thus I'm not
looking for the compiler flags to suppress the warning - I just want to
be able to mark the places I know about). I've used constructs like

data = data;
data++;
if (data != data) return;

And they all work but are a little too obfuscated for my taste. I'm sure
many other people have run into this - is there a recommended technique?

RM
Hi,

With GCC you can use __attribute__((unused))

Regards
--Himanshu
Jun 27 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Daniele Benegiamo | last post by:
Is the following program standard-compliant? I've compiled it with some compilers and no errors or warnings are produced, but this is not a demonstration. The "incriminated" part is the...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
6
by: Chris Stankevitz | last post by:
At link time, MSVC determines some of my libraries are unused and doesn't link them into my exe. This undesirable feature causes problems when I employ the factory pattern. With the factory...
0
by: Steve B. | last post by:
Hello I'm building a Web service that have some methods. I've also two datasets class : DataSet1 and DataSet2. If I add a method in my WS that waits for or return either DataSet1 and...
20
by: Andreas Griesmayer | last post by:
Hi, following piece of code causes a strange behavior when compiled with GCC (Linux, Gentoo 3.3.5.20050130-r1). read() is called twice although only called once, the first time before the first...
11
by: Charles Sullivan | last post by:
I have a number of functions, e.g.: int funct1( int arg1, int arg2, int arg3 ); int funct2( int arg1, int arg2, int arg3 ); int funct3( int arg1, int arg2, int arg3 ); that are called via...
5
by: Glen Buell | last post by:
Hi all, I have a major problem with my ASP.NET website and it's SQL Server 2005 Express database, and I'm wondering if anyone could help me out with it. This site is on a webhost...
2
by: CGatto | last post by:
Hi, We have just started getting the following error during compiles of our forms-based application. We are developing in VS2008, VB.Net, with Team Foundation Server-based source control. ...
11
by: arnuld | last post by:
C takes input character by character. I did not find any Standard Library function that can take a word as input. So I want to write one of my own to be used with "Self Referential Structures" of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.