473,586 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

macros and side effects

Is it true that C macros are unsafe when combined with side effects even if
they are "clean" e.g. could

foo_macro(++c);

execute "++c" multiple times even if foo_macro is "clean"? I have always
thought that "clean" macros (i.e. macros where all parameters are
surrounded by parentheses in the definition) do not have this problem...

I am wondering because I have just looked up putc() in a reference, and it
says that it is unsafe if the "passed" value has a side effect, and I assume
standard macros are "clean"...


May 4 '07 #1
20 3368
>>>>"c" == copx <co**@gazeta.pl writes:

cIs it true that C macros are unsafe when combined with side
ceffects even if they are "clean" e.g. could

cfoo_macro(++c) ;

#define foo_macro(x) (x)*(x)

Whoops.

Charlton

--
Charlton Wilbur
cw*****@chromat ico.net
May 4 '07 #2
copx wrote:
Is it true that C macros are unsafe when combined with side effects even if
they are "clean" e.g. could

foo_macro(++c);

execute "++c" multiple times even if foo_macro is "clean"? I have always
thought that "clean" macros (i.e. macros where all parameters are
surrounded by parentheses in the definition) do not have this problem...

I am wondering because I have just looked up putc() in a reference, and it
says that it is unsafe if the "passed" value has a side effect, and I assume
standard macros are "clean"...
Your reference is correct, and "clean" macros can evaluate their
arguments multiple times. As a simple example, if you have

#define foo_macro(x) ((x) * (x))

then foo_macro's arguments are fully parenthesised, but foo_macro(++c)
will still expand to ((++c) * (++c)).

For most standard library functions, it is required that if they are
also implemented as a macro, that that macro evaluates each argument
exactly once. putc is an exception.

May 4 '07 #3
copx said:
Is it true that C macros are unsafe
Not if written correctly and used correctly.
when combined with side effects even if they are "clean" e.g. could

foo_macro(++c);

execute "++c" multiple times even if foo_macro is "clean"?
That is not a widely-used term for describing macros. (Translation: it's
the first time I have ever seen it.)
I have
always
thought that "clean" macros (i.e. macros where all parameters are
surrounded by parentheses in the definition) do not have this
problem...
You thought wrong.
I am wondering because I have just looked up putc() in a reference,
and it says that it is unsafe if the "passed" value has a side effect,
Your reference says wrong. The putc function, if implemented as a macro,
may evaluate the stream more than once, but *not* the character. And
you're hardly likely to call it as putc('\n', fp++), I trust?
and I assume standard macros are "clean"...
Why?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 4 '07 #4
Richard Heathfield wrote:
copx said:
I am wondering because I have just looked up putc() in a reference,
and it says that it is unsafe if the "passed" value has a side effect,

Your reference says wrong. The putc function, if implemented as a macro,
may evaluate the stream more than once, but *not* the character.
That's a useful clarification, but it doesn't mean the reference is
wrong.

May 4 '07 #5
Richard Heathfield wrote On 05/04/07 12:34,:
copx said:
>>[...] "clean" macros (i.e. macros where all parameters are
surrounded by parentheses in the definition) [...]

and I assume standard macros are "clean"...


Why?
Perhaps because of the Standard? 6.10.8 lists the
values of the predefined macros, and all are "clean."
7.1.2p5 requires "cleanlines s" of all object-like macros
defined in the standard headers, and 7.1.4p2 does the same
for function-like macros that implement standard library
functions. What remains are the dribs and drabs like
va_start() and setjmp(); do you find them "unclean" (in
the O.P.'s sense, that is)?

--
Er*********@sun .com
May 4 '07 #6
In article <gP************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>copx said:
>Is it true that C macros are unsafe

Not if written correctly and used correctly.
It's worth noting, though, that they're one of the easier parts of the
language to get wrong unintentionally , and it's sometimes easy to convert
correct use into incorrect use without realizing it, even if you do know
what you're doing.

If you compare programming in C to running with scissors, a lot of code
that uses macros ends up being at the pointy end of the scissors.
dave
(runs around the pool with scissors)

--
Dave Vandervies dj******@csclub .uwaterloo.ca
C is for people who run with scissors on a daily basis without cutting
themselves, because they know how to be careful around sharp tools.
--Richard Heathfield in comp.lang.c
May 4 '07 #7
copx wrote:
Is it true that C macros are unsafe when combined with side effects
even if they are "clean" e.g. could

foo_macro(++c);

execute "++c" multiple times even if foo_macro is "clean"?
Yes.
I have
always
thought that "clean" macros (i.e. macros where all parameters are
surrounded by parentheses in the definition) do not have this
problem...
Because macros are essentially a form of text substitution, the
parentheses in a macro definition can not influence how often an
argument gets evaluated.

The parentheses around macro arguments are needed to ensure that the
parser always interprets the complete argument as a single expression.
For example, take these two macros:

#define HALVE(x) x/2
#define DIVIDE_BY_TWO(x ) (x)/2

When you use them like this:

y = HALVE(3+1);
z = DIVIDE_BY_TWO(3 +1);

the preprocessor will replace the macros, yielding:

y = 3+1/2;
z = (3+1)/2;

Do you see the difference between the two statements?
>
I am wondering because I have just looked up putc() in a reference,
and it says that it is unsafe if the "passed" value has a side effect,
and I assume standard macros are "clean"...
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
May 4 '07 #8

"Bart van Ingen Schenau" <ba**@ingen.ddn s.infoschrieb im Newsbeitrag
news:60******** ********@ingen. ddns.info...
copx wrote:
>Is it true that C macros are unsafe when combined with side effects
even if they are "clean" e.g. could

foo_macro(++c) ;

execute "++c" multiple times even if foo_macro is "clean"?

Yes.
>I have
always
thought that "clean" macros (i.e. macros where all parameters are
surrounded by parentheses in the definition) do not have this
problem...

Because macros are essentially a form of text substitution, the
parentheses in a macro definition can not influence how often an
argument gets evaluated.

The parentheses around macro arguments are needed to ensure that the
parser always interprets the complete argument as a single expression.
For example, take these two macros:

#define HALVE(x) x/2
#define DIVIDE_BY_TWO(x ) (x)/2

When you use them like this:

y = HALVE(3+1);
z = DIVIDE_BY_TWO(3 +1);

the preprocessor will replace the macros, yielding:

y = 3+1/2;
z = (3+1)/2;

Do you see the difference between the two statements?
[snip]

Yes, I remember that much of kindergarten math ;)

Thanks everyone.


May 4 '07 #9

"Harald van D?k" <tr*****@gmail. comwrote in message
news:11******** **************@ c35g2000hsg.goo glegroups.com.. .
copx wrote:
>Is it true that C macros are unsafe when combined with side effects even
if
they are "clean" e.g. could

foo_macro(++c) ;

execute "++c" multiple times even if foo_macro is "clean"? I have always
thought that "clean" macros (i.e. macros where all parameters are
surrounded by parentheses in the definition) do not have this problem...

I am wondering because I have just looked up putc() in a reference, and
it
says that it is unsafe if the "passed" value has a side effect, and I
assume
standard macros are "clean"...

Your reference is correct, and "clean" macros can evaluate their
arguments multiple times. As a simple example, if you have

#define foo_macro(x) ((x) * (x))

then foo_macro's arguments are fully parenthesised, but foo_macro(++c)
will still expand to ((++c) * (++c)).

For most standard library functions, it is required that if they are
also implemented as a macro, that that macro evaluates each argument
exactly once. putc is an exception.
Not "exactly once", but "at most once". It is quite legitimate for
some argument to not be evaluated at all.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
May 4 '07 #10

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

Similar topics

16
2406
by: mike420 | last post by:
Tayss wrote: > > app = wxPySimpleApp() > frame = MainWindow(None, -1, "A window") > frame.Show(True) > app.MainLoop() > Why do you need a macro for that? Why don't you just write
8
7220
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to their use as 'functions'; I realise the loss of type-safety and the possible evaluation errors that can occur. However, what would be the harm with...
3
1361
by: Generic Usenet Account | last post by:
I have written a small macro that provides the relative offset of any field within a structure. Here it goes: #define RELATIVE_OFFSET(a,b) \ { \ cout << "The relative offset of the " \ << strchr(#b, '.') + 1 << " field is " \ << (int)&b - (int)&a << endl; \ }
5
3214
by: Niklaus | last post by:
This is one of the posts that i got. ------------------------------ A "side effect" of an operation is something that *happens*, not something that *is produced*. Examples: In the expression 2+2, the value 4 *is produced*. Nothing *happens*. Thus, 4 is the value of the expression, and it has no side effects. In the expression g=2.0, the...
2
7590
by: Andrew Arro | last post by:
is it possible to make smth like a loop of macroses? i.e. i want some macros to be called X times, all that on the PREPROCESSOR lever i was trying smth like #define vv_0 100 #define vv_1 101 #define vv_2 102
33
8861
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to write type generic functions macros? #define CUBE(I) ( (I) * (I) * (I) ) Is there some more concise set of things where inline functions should...
27
2615
by: Cephalobus_alienus | last post by:
Hello, I know that macros are evil, but I recently came across a problem that I couldn't figure out how to solve with templates. I wanted to create a set of singleton event objects, and wrote the following macro: #define GET_SINGLETON_EVENT_FUNCTION(FUNCTION_NAME,MANUAL_RESET,\ INITIAL_STATE,EVENT_NAME) ...
3
1506
by: Floobar | last post by:
Macros sure can be fun -- and profitable. This actually worked -- it might work for any of you guys too. The trick is to make your code look sensible, but be actually very hard to modify without introducing unexpected side effects, so that anyone hired to replace you will resign, shoot himself, jump off a bridge, or some such within weeks...
6
1435
by: prashant.khade1623 | last post by:
HI All, I know the clear distinction between macro and function. I know that macro will speed up the program and using function will reduce the size. But how do we know when to use macro and function. ?
0
7912
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
8338
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
7959
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
8216
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...
0
6614
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...
1
5710
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...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1180
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.