473,573 Members | 3,961 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suppressing "Parameter not used" Warning

Please note crosspost.

Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:

typedef void (*State_Fn)(uin t8_t);

void error_state(uin t8_t status)
{
NOT_USED(status );

/* code handling error but ignoring status */
}

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))

He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code. I for one had never seen the former before,
though I've often seen the latter (usually not hidden behind a macro),
and I've never seen it actually generate code. At least, not in the
last ten years or so.

So I'm curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?

Thanks,
-=Dave

-=Dave
--
Change is inevitable, progress is not.
Nov 15 '05 #1
40 7836
Dave Hansen wrote:

Please note crosspost.

Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:

typedef void (*State_Fn)(uin t8_t);

void error_state(uin t8_t status)
{
NOT_USED(status );

/* code handling error but ignoring status */
}

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))

He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code.
Non executable code tends to generate warnings.
I for one had never seen the former before,
though I've often seen the latter (usually not hidden behind a macro),
and I've never seen it actually generate code. At least, not in the
last ten years or so.

So I'm curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?


In distributions file for a sort timing program
my distribution function arguments are of this form:
(e_type *array, size_t n, long unsigned *seed)

but only some of them use the seed for a PRNG.

Others are like this:

void sorted(e_type *a, size_t n, long unsigned *seed)
{
a += n;
while (n-- != 0) {
(*--a).data = n;
}
seed;
}

So, I just make an expression statement out of seed
and that seems to stop the warnings.

--
pete
Nov 15 '05 #2
In comp.lang.c Dave Hansen <id**@hotmail.c om> wrote:
typedef void (*State_Fn)(uin t8_t); void error_state(uin t8_t status)
{
NOT_USED(status ); /* code handling error but ignoring status */
}


Why not just

void error_state( uint8_t ) /* don't care about formal parameter */
{
/* code */
}

? (I'm not enough of a guru to answer your real question.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #3
Christopher Benson-Manica wrote:
In comp.lang.c Dave Hansen <id**@hotmail.c om> wrote:

typedef void (*State_Fn)(uin t8_t);


void error_state(uin t8_t status)
{
NOT_USED(status );


/* code handling error but ignoring status */
}

Why not just

void error_state( uint8_t ) /* don't care about formal parameter */
{

Because that's not legal C. If it were, we obviously wouldn't need any
hacks. You can do this in prototypes; in C++ you can also do it in
definitions. Not in C, however.

S.
Nov 15 '05 #4


Dave Hansen wrote On 10/03/05 10:13,:
Please note crosspost.

Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:

typedef void (*State_Fn)(uin t8_t);

void error_state(uin t8_t status)
{
NOT_USED(status );

/* code handling error but ignoring status */
}

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))

He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code. I for one had never seen the former before,
though I've often seen the latter (usually not hidden behind a macro),
and I've never seen it actually generate code. At least, not in the
last ten years or so.

So I'm curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?


First, there is no sure-fire way to prevent compilers
from issuing diagnostics. The compiler is entitled to
grouse about anything it chooses, provided it accepts code
that does not actually contravene the Standard. It can
warn about spellnig errors in comennts, or about inconsistent
indentation levels. The requirement "No warnings from any
compiler" is not ultimately tenable.

FWIW, the `(void)p' formulation seems to be widespread.
Even if a compiler complains about it, a human reader will
see immediately that it was in fact the programmer's intent
that `p' remain unused -- the programmer may have made a
mistake, but at least it was not one of simple inattention.

I don't think `(p)=(p)' is a wonderful idea. If `p' is
volatile the generated code must perform both the read and the
write. If `p' is `const' the compiler is required to issue a
diagnostic, so you're no better off than when you started --
worse, if anything. Of course, `const'-qualified function
parameters are fairly unusual and `volatile' parameters are
exceedingly rare, but the possibilities exist.

In any case, hiding the actual trickery behind a NOT_USED
macro seems a good idea: you can re-#define NOT_USED as part
of your adaptation to each new compiler that comes along,
using whatever compiler-specific dodge seems to work best.

--
Er*********@sun .com

Nov 15 '05 #5
In article <1128348834.7e3 4f2c71121565d6e 8683d1777b7524@ teranews>,
Dave Hansen <id**@hotmail.c om> wrote:

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning.


An alternative would be to invoke the compiler using a flag or option
that disables the unused variable/parameter warning.

However, it's good practice to turn the warning back on every once
in a while and see if any unexpected unused thingies have crept into
the code.
Nov 15 '05 #6
Dave Hansen wrote:
Please note crosspost.

Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:

typedef void (*State_Fn)(uin t8_t);

void error_state(uin t8_t status)
{
NOT_USED(status );

/* code handling error but ignoring status */
}

In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))

He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code. I for one had never seen the former before,
though I've often seen the latter (usually not hidden behind a macro),
and I've never seen it actually generate code. At least, not in the
last ten years or so.

So I'm curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?

Thanks,
-=Dave

-=Dave


I use the first version, a cast-to-void macro. It works fine for gcc
(various ports).
Nov 15 '05 #7
On 2005-10-03, David Brown <da***@westcont rol.removethisb it.com> wrote:
#define NOT_USED(p) ((void)(p))
[...]
#define NOT_USED(p) ((p)=(p))
[...]

So I'm curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?
I use the first version, a cast-to-void macro. It works fine
for gcc (various ports).


Very slightly OT, but I just use gcc's __attribute__(( unused)).
I realize it's not-portable to other compilers, but...

1) In my applications so much of the code is platform-specific
that it just doesn't matter.

2) I've used nothing but gcc for embedded work for the past 6
or 7 years anyway.

--
Grant Edwards grante Yow! Will the third world
at war keep "Bosom Buddies"
visi.com off the air?
Nov 15 '05 #8
In comp.lang.c Skarmander <in*****@dontma ilme.com> wrote:
Because that's not legal C. If it were, we obviously wouldn't need any
hacks. You can do this in prototypes; in C++ you can also do it in
definitions. Not in C, however.


My apologies; I use C++, and this was a difference of which I was not
aware. Thanks.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #9
Dave Hansen wrote:
In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was

#define NOT_USED(p) ((void)(p))

In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use

#define NOT_USED(p) ((p)=(p))
So I'm curious. Which form (if either) is more common? Are there
any implementations that will generate executable code for the latter?


Yes, the compiler might warn that 'p' is assigned a value which
is never used. And it might also warn about reading an uninitialized
variable.

One compiler I use has this definition:

#define NOT_USED(junk) { (volatile typeof(junk))ju nk = junk; }

Nov 15 '05 #10

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

Similar topics

8
5720
by: harry | last post by:
Hi, During compilation, a C# project in my solution triggers the following warning: "warning CS0168: The variable 'ex' is declared but never used" To trigger this warning, it appears the C# compiler WarningLevel needs to be 3 or above.
11
23213
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 pointers in a table, with the same parameters regardless of the particular function. In some of the functions, one or more of the
0
7792
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7709
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8039
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8218
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7800
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6435
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3744
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.