473,326 Members | 2,134 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,326 software developers and data experts.

Supressing Unused Parameter Warnings

Is there a standard method for supressing warnings regarding unused
parameters? I have a function that might be called hundreds of thousands
of times that looks like this:

const void *
idx_byte(const void *p, int idx, void *context)
{
return (unsigned char *)p + idx;
}

This generates a warning that the "context" parameter is unused. If I
insert:

context = NULL;

to supress that will that potentially impact performace? With GCC I know
you can do:

const void *
idx_byte(const void *p, int idx, void * __attribute__ ((unused)) context)
{
return (unsigned char *)p + idx;
}

I'm not sure which is uglier but is there a standard method for handling
this?

Also, this function is a member of a structure possibly supplied by the
caller so making it 'inline' will have no impact right? Is there a way
to improve the performance of this function?

Thanks,
Mike
Nov 14 '05 #1
11 8025

On Fri, 7 May 2004, Michael B Allen wrote:

Is there a standard method for supressing warnings regarding unused
parameters?
No. Because there is no standard diagnostic about unused parameters.
It's the same sort of situation we have with assignments in 'if'
statements (that's probably in the FAQ, but I can't find it). The
"unused parameter" warning is purely a "quality of implementation"
issue, as is any way your compiler might provide to shut it off.
If I insert:

context = NULL;

to supress that will that potentially impact performace?
Highly unlikely. You can find out by examining the generated assembly
or machine code from your compiler(s). Any decent compiler should be
able to figure out that that assignment is irrelevant, and compile it
out.
On GCC, you can also write

context;

to trade the "unused argument" warning for a "statement has no effect"
warning; or

(void)context;

to remove the warning altogether. Or, probably the best option, you
can simply pass the -Wno-unused option to the GCC compiler in the
first place. All this is explained in the GCC documentation, and
questions related to it ought to go to gnu.gcc.help, not comp.lang.c.
With GCC I know you can do:

const void *
idx_byte(const void *p, int idx, void * __attribute__ ((unused)) context)
In some contexts, it would make sense to #define UNUSED depending
on the compiler: if you could tell you were using GCC, then
#define UNUSED __attribute__((unused))
otherwise
#define UNUSED
This is not a standard answer to the problem, but then there is no
standard answer because it's not a standard problem --- it's a GCC
problem! :)
Also, this function is a member of a structure
Not in C, it's not. Functions are functions. Structure members
are data. Never the twain shall meet. Maybe you meant that this
function could be *pointed to* by a structure member of function-pointer
type, but I don't see how that's relevant.
possibly supplied by the caller
Caller of what?
so making it 'inline' will have no impact right? Is there a way
to improve the performance of this function?


Yes, but it's also non-standard: turn up the optimization levels
on your compiler. ;) There's not much the programmer can do to
expedite a single addition!

-Arthur
Nov 14 '05 #2
Michael B Allen <mb*****@ioplex.com> writes:
With GCC I know you can do:

const void *
idx_byte(const void *p, int idx, void * __attribute__ ((unused)) context)
{
return (unsigned char *)p + idx;
}

I'm not sure which is uglier but is there a standard method for handling
this?
I usually do something like this:
#ifdef __GNUC__
#define UNUSED __attribute__ ((unused))
#else
#define UNUSED
#endif
....
void foo (int x UNUSED)
(There is nothing preventing other compilers from defining
__GNUC__, except legions of irate programmers.)
Also, this function is a member of a structure possibly supplied by the
caller so making it 'inline' will have no impact right? Is there a way
to improve the performance of this function?


Without a broader context I can see no way to improve the
performance of a function that essentially does `return p[idx];'.
Do you actually know that this is a bottleneck? Have you
profiled your program?
--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."
--Michael Girdwood, in the monastery
Nov 14 '05 #3
"Michael B Allen" <mb*****@ioplex.com> wrote in message
news:pa**********************************@ioplex.c om...
Is there a standard method for supressing warnings regarding unused
parameters? I have a function that might be called hundreds of thousands
of times that looks like this:

const void *
idx_byte(const void *p, int idx, void *context)
{
No standard method that I know of, but I use:

(void)context;

This works for at least GCC and, unlike the extension for this purpose that
GCC provides, will compile with any compiler.
return (unsigned char *)p + idx;
}

This generates a warning that the "context" parameter is unused. If I
insert:

context = NULL;

to supress that will that potentially impact performace?


Perhaps, but probably not if optimisations are turned on. On at least one
compiler I have used this won't really help, as it will replace the unused
warning with a useless assignment warning.

Alex
Nov 14 '05 #4
In article <pa**********************************@ioplex.com >,
Michael B Allen <mb*****@ioplex.com> wrote:
Is there a standard method for supressing warnings regarding unused
parameters? I have a function that might be called hundreds of thousands
of times that looks like this:

const void *
idx_byte(const void *p, int idx, void *context)
{
return (unsigned char *)p + idx;
}

This generates a warning that the "context" parameter is unused. If I
insert:

context = NULL;

to supress that will that potentially impact performace?


There are several possibilities:

a. Performance is no concern, so it doesn't matter.

b. Performance is a concern, but it doesn't impact performance, so it is
fine.

c. Performance is a concern, and your compiler is so braindamaged stupid
that it generates code for this assignment. In that case, don't change
the C code, change the compiler.
Nov 14 '05 #5
"Michael B Allen" <mb*****@ioplex.com> wrote in message
news:pa**********************************@ioplex.c om...
Is there a standard method for supressing warnings regarding unused
parameters?
No, but most compilers allow you to selectively supress certain warnings. If your compiler
has this option, then I suggest you use that rather than modify your code.
I have a function that might be called hundreds of thousands
of times that looks like this:

const void *
idx_byte(const void *p, int idx, void *context)
{
return (unsigned char *)p + idx;
[BTW, you cast away the constness of p here, only to restore it. Not a problem per se, but
not aesthetically a good idea IMHO.]
}


The obvious question is: why have you (or the author) chosen a function signature where
one parameter is unused? I dare say you'll achieve _noticable_ improvements if the
redundant parameter was removed.

If you're anticipating future requirements, then I suggest you just ignore the warnings
for the time being.

That said, one way you could improve such a simple function is by replacing it with a
macro...

#define idx_byte(p, idx, context) \
((const void *) ((const unsigned char *) (p) + (int) (idx)))

--
Peter
Nov 14 '05 #6
On Fri, 07 May 2004 19:10:14 -0400, Ben Pfaff wrote:
With GCC I know you can do:

const void *
idx_byte(const void *p, int idx, void * __attribute__ ((unused))
context) {
return (unsigned char *)p + idx;
}

I'm not sure which is uglier but is there a standard method for
handling this?


I usually do something like this:
#ifdef __GNUC__
#define UNUSED __attribute__ ((unused))
#else #define UNUSED
#endif
...
void foo (int x UNUSED)
(There is nothing preventing other compilers from defining __GNUC__,
except legions of irate programmers.)


I like it!
Also, this function is a member of a structure possibly supplied by the
caller so making it 'inline' will have no impact right? Is there a way
to improve the performance of this function?


Without a broader context I can see no way to improve the performance of
a function that essentially does `return p[idx];'. Do you actually know
that this is a bottleneck? Have you profiled your program?


Well I don't need to profile it to know it's called *a lot* when the
input data set is big so any improvement at all would probably impact
the bottom line. Would 'restrict' help?

Thanks,
Mike
Nov 14 '05 #7
On Fri, 07 May 2004 20:46:34 -0400, Peter Nilsson wrote:
I have a function that might be called hundreds of thousands of times
that looks like this:

const void *
idx_byte(const void *p, int idx, void *context) {
return (unsigned char *)p + idx;
[BTW, you cast away the constness of p here, only to restore it. Not a
problem per se, but not aesthetically a good idea IMHO.]


Hmm, yes. I suppose I could do away with the case altogether.
The obvious question is: why have you (or the author) chosen a function
signature where one parameter is unused? I dare say you'll achieve
_noticable_ improvements if the redundant parameter was removed.

If you're anticipating future requirements, then I suggest you just
ignore the warnings for the time being.

That said, one way you could improve such a simple function is by
replacing it with a macro...

#define idx_byte(p, idx, context) \
((const void *) ((const unsigned char *) (p) + (int) (idx)))


Can't. I've implemented a sequence comparison algo (shortest edit sequence
a.k.a diff) and I want it to be generic so I permit the user to supply
their sequences as const void * with index and compare function pointers
to index and compare elements.

Mike
Nov 14 '05 #8
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
On Fri, 7 May 2004, Michael B Allen wrote:
Is there a standard method for supressing warnings regarding unused
parameters?
[...] Or, probably the best option, you can simply pass the -Wno-unused
option to the GCC compiler in the first place. All this is
explained in the GCC documentation, and questions related to it
ought to go to gnu.gcc.help, not comp.lang.c.


Presumably this would suppress *all* warnings for unused parameters
(and variables, etc.), rather than for the one that you're interested
in. Wholesale suppression of warnings can be dangerous.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #9
Peter Nilsson wrote:
...
The obvious question is: why have you (or the author) chosen a function signature where
one parameter is unused? I dare say you'll achieve _noticable_ improvements if the
redundant parameter was removed.

If you're anticipating future requirements, then I suggest you just ignore the warnings
for the time being.
...


One obvious reason to choose such function signature is when this
signature is imposed by some independent code. A callback function's
signature, for example, is determined by the caller.

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #10
Michael B Allen <mb*****@ioplex.com> writes:
On Fri, 07 May 2004 19:10:14 -0400, Ben Pfaff wrote:

[...]
Also, this function is a member of a structure possibly supplied by the
caller so making it 'inline' will have no impact right? Is there a way
to improve the performance of this function?


Without a broader context I can see no way to improve the performance of
a function that essentially does `return p[idx];'. Do you actually know
that this is a bottleneck? Have you profiled your program?


Well I don't need to profile it to know it's called *a lot* when the
input data set is big so any improvement at all would probably impact
the bottom line. Would 'restrict' help?


Ok, but do you actually know that this is a bottleneck? Have you
profiled your program?

(I don't see how restrict would help, but profiling can tell you
whether it actually does or no.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #11
Michael B Allen wrote:

Is there a standard method for supressing warnings regarding
unused parameters? I have a function that might be called
hundreds of thousands of times that looks like this:

const void *
idx_byte(const void *p, int idx, void *context)
{
return (unsigned char *)p + idx;
}

This generates a warning that the "context" parameter is unused.


Add the line:

context;

somewhere in the body. It effectively loads and discards the
parameter value. You may even cast it to void, as in
"(void)context;" if you prefer. The optimizer should discard any
code loaded, and even if not the run-time cost is small.

Some people wrap this in a macro, such as:

#define UNUSED(x) x

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #12

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

Similar topics

1
by: Jp Calderone | last post by:
When using PyChecker on a project which uses bsddb, dozens of messages like the following are reported: warning: couldn't find real module for class bsddb._db.DBAccessError (module name:...
2
by: Mark Harrison | last post by:
Due to some uninteresting configuration management issues, I'm running a mismatched python library and getting this message dumped to stderr. > /.../lib/site-python/Sybase/Sybase.py:20: >...
15
by: Eirik | last post by:
This is a little function I wrote, inspired by the thread "Urgent HELP! required for Caesar Cipher PLEASE" $ cat /home/keisar/bin/c/ymse/rot13.h char rot13(char character) { int changed;...
12
by: zacks | last post by:
Suddenly, in a VB2005 project I am working on, several variables show up in the list of warnings are being unused local variables. But they all are. Several of them are the ex variable used in a...
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...
8
by: Frank Rizzo | last post by:
Is there a setting in VS2005 to quickly locate methods that are unused (maybe through compiler warnings)? If not, any utilities out there that do that? Thanks
9
by: werasm | last post by:
Hi all, I'm looking for a nice clean (portable) way to get rid of unused variable warnings without fiddling with compiler settings (on a per case basis). I've come up with this: template...
33
by: jacob navia | last post by:
Hi I am considering extending the lcc-win32 compiler to accept int fn(int a, int b,double) { // body of the function } This allows the programmer to specify that the third parameter is not...
13
by: Rex Mottram | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.