Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Supressing Unused Parameter Warnings

Reply
Thread Tools

Supressing Unused Parameter Warnings

 
 
Michael B Allen
Guest
Posts: n/a
 
      05-07-2004
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
 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      05-07-2004

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
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      05-07-2004
Michael B Allen <(E-Mail Removed)> 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
 
Reply With Quote
 
Alex Fraser
Guest
Posts: n/a
 
      05-07-2004
"Michael B Allen" <(E-Mail Removed)> wrote in message
news(E-Mail Removed) 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


 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      05-07-2004
In article <(E-Mail Removed) >,
Michael B Allen <(E-Mail Removed)> 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.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      05-08-2004
"Michael B Allen" <(E-Mail Removed)> wrote in message
news(E-Mail Removed) 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


 
Reply With Quote
 
Michael B Allen
Guest
Posts: n/a
 
      05-08-2004
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
 
Reply With Quote
 
Michael B Allen
Guest
Posts: n/a
 
      05-08-2004
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-08-2004
"Arthur J. O'Dwyer" <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      05-08-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get rid of unused parameter warning? Rui Maciel C++ 14 10-01-2010 08:34 AM
any way to avoid warnings about unused outputs in XST? Ken Cecka VHDL 10 03-02-2009 10:51 PM
Clean up "unused parameter" compiler warnings? Charles Sullivan C Programming 11 09-11-2006 09:34 AM
use warnings; and use Warnings; give different results Ted Sung Perl Misc 1 08-30-2004 10:22 PM
Supressing PyChecker warnings Jp Calderone Python 1 01-16-2004 09:07 PM



Advertisments