473,746 Members | 2,262 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
40 7883
"Old Wolf" <ol*****@inspir e.net.nz> writes:
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.


In fact, strictly speaking, this could invoke undefined behavior if p
really hasn't been initialized. Since it's a parameter (at least in
the context intended by the OP), that would happen only if the caller
passes an uninitialized value, so it would require some additional
smarts by the compiler to detect the problem.

On most real-world systems, this isn't going to cause any visible
problems, but it's possible that certain pointer values can cause a
trap when you try to load them into a register, even if there's no
attempt to dereference the value. The same could apply to any type
that can have trap representations .

Hiding this behind a NOT_USED macro is a good idea. The definition of
the macro can probably be made to vary depending on what system you're
on, using the usual twisty maze of #ifdefs method.

The ((p)=(p)) trick is ok if you make sure p is always initialized,
even to a meaningless value.

As others have mentioned, there is no portable way to control specific
warnings that will work across implementations .

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #11
In article <11************ **********@g47g 2000cwa.googleg roups.com>,
ol*****@inspire .net.nz says...
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; }

Metrowerks Codewarrior has the

#pragma unused (varname)

construct to solve this problem. I'm surprised that other compilers
don't have the same facility.

Mark Borgerson
Nov 15 '05 #12
Dave Hansen wrote:
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 have used a void cast for some compilers. For another, I was
successful with

#define UNUSED(x) if(x);

Thad

Nov 15 '05 #13
"David Brown" <da***@westcont rol.removethisb it.com> wrote in message
news:43******** @news.wineasy.s e...
....
#define NOT_USED(p) ((void)(p))
.... I use the first version, a cast-to-void macro. It works fine for gcc
(various ports).


Also: borland, watcom, a few less popilar ones. I don't remember about
msvc++. Probably too.
Alex
Nov 15 '05 #14
"Thad Smith" <Th*******@acm. org> wrote in message
news:43******** *************** @auth.newsreade r.octanews.com. ..
....
I have used a void cast for some compilers. For another, I was
successful with

#define UNUSED(x) if(x);


Nice, though some clever compiler could warn here whether or not you're sure
it's what you intend to do, whether or not this code has any effect.

Alex
Nov 15 '05 #15
Mark Borgerson wrote:
In article <11************ **********@g47g 2000cwa.googleg roups.com>,
ol*****@inspire .net.nz says...
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
implementati on 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; }


Metrowerks Codewarrior has the

#pragma unused (varname)

construct to solve this problem. I'm surprised that other compilers
don't have the same facility.

Mark Borgerson


The trouble with such pragmas is that they are completely non-portable.
As Grant Edwards said, gcc has an equivalent (using an attribute,
which is gcc prefers over pragmas). Often non-portability is not a
problem, since there are so many other non-portable aspects to typical
embedded systems (and in the case of gcc, it is at least portable to a
few dozen other gcc ports).
Nov 15 '05 #16
Grant Edwards wrote:
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
implementati ons 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.


Also OT, but the warning that bugs me most with gcc is "warning: will
never be executed" on code that gets removed by the optimiser. If I've
written code that really cannot ever be executed, it's (almost
certainly) a mistake, so I want the compiler to tell me. But on code
like this:

if (test()) {
doThis();
doThat();
} else {
doSomethingElse ();
doThat();
}

gcc will combine the two "doThat()" calls (which is good), and then warn
that one of the "will never be executed", which is bad.

If you know any good ideas to get working "will never be executed"
warnings, even if it is "wait for gcc 4.1.x", I'd love to hear them.
Nov 15 '05 #17
"Dave Hansen" <id**@hotmail.c om> wrote in message
news:1128348834 .7e34f2c7112156 5d6e8683d1777b7 524@teranews...
Please note crosspost.


snip snip

void error_state(uin t8_t /* status */)

I prefer to leave the argument there as it should make code easier to
comprehend. Commenting it out does not need comments.
Nov 15 '05 #18
David Brown wrote:
if (test()) {
doThis();
doThat();
} else {
doSomethingElse ();
doThat();
}

gcc will combine the two "doThat()" calls (which is good), and then warn
that one of the "will never be executed", which is bad.

If you know any good ideas to get working "will never be executed"
warnings, even if it is "wait for gcc 4.1.x", I'd love to hear them.


Hmmm. I tried this with a couple of GCC versions at default
and -O[23] optimization levels (cross and native) and saw no
such warning. What options/versions are you using?

--
Michael N. Moran (h) 770 516 7918
5009 Old Field Ct. (c) 678 521 5460
Kennesaw, GA, USA 30144 http://mnmoran.org

"So often times it happens, that we live our lives in chains
and we never even know we have the key."
The Eagles, "Already Gone"

The Beatles were wrong: 1 & 1 & 1 is 1

Nov 15 '05 #19
Bill Davy wrote:
"Dave Hansen" <id**@hotmail.c om> wrote in message
news:1128348834 .7e34f2c7112156 5d6e8683d1777b7 524@teranews...
Please note crosspost.


snip snip

void error_state(uin t8_t /* status */)

I prefer to leave the argument there as it should make code easier to
comprehend. Commenting it out does not need comments.


As noted by an earlier poster, that is not valid for a C function
definition (declaration yes, but not definition which is what was being
discussed).

If you want to discus what you do in C++ (or any language other than C)
then please remove comp.lang.c from the crosspost.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #20

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

Similar topics

8
5742
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
23239
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
8975
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8801
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9516
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9351
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9219
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6774
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4840
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2200
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.