Supressing Unused Parameter Warnings

  • Thread starter Michael B Allen
  • Start date
M

Michael B Allen

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
 
A

Arthur J. O'Dwyer

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
 
B

Ben Pfaff

Michael B Allen said:
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?
 
A

Alex Fraser

Michael B Allen said:
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
 
C

Christian Bau

Michael B Allen said:
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.
 
P

Peter Nilsson

Michael B Allen said:
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)))
 
M

Michael B Allen

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
 
M

Michael B Allen

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
 
K

Keith Thompson

Arthur J. O'Dwyer said:
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.
 
A

Andrey Tarasevich

Peter said:
...
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.
 
K

Keith Thompson

Michael B Allen said:
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.)
 
C

CBFalconer

Michael said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top