473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with ABS() macro

K.N. King's book "C Programming: A Modern Approach" gives this exercise
in chapter 14, which is about the preprocessor:
The following macro has a subtle problem:

#define ABS(a) ((a)<0?-(a):a)

Give an example that shows why ABS doesn't work,
and show how to fix the problem. You may assume
that the argument to ABS doesn't have a side effect.
The only potential problem I see is when INT_MIN is passed, which I do
not believe to be the solution that King has in mind. Can anyone shed
some light on this problem? Most of the other problems in the chapter
are related to not parenthesizing the arguments properly, but I can't
think of a case where the missing parenthesis could causse a problem in
the above macro.

Thanks.
-Peter
--
Pull out a splinter to reply.
Nov 14 '05 #1
7 16190
Peter Ammon wrote:
K.N. King's book "C Programming: A Modern Approach" gives this exercise
in chapter 14, which is about the preprocessor:
The following macro has a subtle problem:

#define ABS(a) ((a)<0?-(a):a)

Give an example that shows why ABS doesn't work,
and show how to fix the problem. You may assume
that the argument to ABS doesn't have a side effect.
The only potential problem I see is when INT_MIN is passed, which I do
not believe to be the solution that King has in mind. Can anyone shed
some light on this problem? Most of the other problems in the chapter
are related to not parenthesizing the arguments properly, but I can't
think of a case where the missing parenthesis could causse a problem in
the above macro.


Hint: Can, you, think, of, any, C, operator, without,
side, effects, but, with, lower, prececence, than, `?:'?

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 14 '05 #2
The main thing is not to get hooked into considering only examples of
ABS(a) where a is a simple variable name.

Nov 14 '05 #3
Eric Sosman <es*****@acm-dot-org.invalid> scribbled the following:
Peter Ammon wrote:
K.N. King's book "C Programming: A Modern Approach" gives this exercise
in chapter 14, which is about the preprocessor:

The following macro has a subtle problem:

#define ABS(a) ((a)<0?-(a):a)

Give an example that shows why ABS doesn't work,
and show how to fix the problem. You may assume
that the argument to ABS doesn't have a side effect.

The only potential problem I see is when INT_MIN is passed, which I do
not believe to be the solution that King has in mind. Can anyone shed
some light on this problem? Most of the other problems in the chapter
are related to not parenthesizing the arguments properly, but I can't
think of a case where the missing parenthesis could causse a problem in
the above macro.
Hint: Can, you, think, of, any, C, operator, without,
side, effects, but, with, lower, prececence, than, `?:'?


So if we change the macro to:
#define ABS(a) ((a)<0 ? -(a) : (a))
and remember the assumption that the argument won't have a side effect,
then it should work?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"We sorcerers don't like to eat our words, so to say."
- Sparrowhawk
Nov 14 '05 #4
Eric Sosman wrote:
Peter Ammon wrote:
K.N. King's book "C Programming: A Modern Approach" gives this
exercise in chapter 14, which is about the preprocessor:
The following macro has a subtle problem:

#define ABS(a) ((a)<0?-(a):a)

Give an example that shows why ABS doesn't work,
and show how to fix the problem. You may assume
that the argument to ABS doesn't have a side effect.
The only potential problem I see is when INT_MIN is passed, which I do
not believe to be the solution that King has in mind. Can anyone shed
some light on this problem? Most of the other problems in the chapter
are related to not parenthesizing the arguments properly, but I can't
think of a case where the missing parenthesis could causse a problem
in the above macro.

Hint: Can, you, think, of, any, C, operator, without,
side, effects, but, with, lower, prececence, than, `?:'?


Yes, the comma operator. But ABS(0, -1) is treated as sending two
arguments to the ABS operator, not

--
Pull out a splinter to reply.
Nov 14 '05 #5
Eric Sosman wrote:
Peter Ammon wrote:
K.N. King's book "C Programming: A Modern Approach" gives this
exercise in chapter 14, which is about the preprocessor:
The following macro has a subtle problem:

#define ABS(a) ((a)<0?-(a):a)

Give an example that shows why ABS doesn't work,
and show how to fix the problem. You may assume
that the argument to ABS doesn't have a side effect.
The only potential problem I see is when INT_MIN is passed, which I do
not believe to be the solution that King has in mind. Can anyone shed
some light on this problem? Most of the other problems in the chapter
are related to not parenthesizing the arguments properly, but I can't
think of a case where the missing parenthesis could causse a problem
in the above macro.

Hint: Can, you, think, of, any, C, operator, without,
side, effects, but, with, lower, prececence, than, `?:'?


(Sorry for the double post, I hit send by mistake)

Yes, the comma operator. But ABS(0, -1) is parsed as attempting to
invoke ABS with two parameters rather than a single comma-parameter, and
ABS((0, -1)) works as expected because then the argument is already
parenthesized.

(Thinking...)

I can produce an error with the double-macro trick:

#define x 0,-1
#define ABS(a) ((a)<0?-(a):a)

Then ABS(x) is -1.

I'll have to give the book more credit if that's the solution the author
was looking for!

-Peter
--
Pull out a splinter to reply.
Nov 14 '05 #6
On Wed, 29 Dec 2004 14:14:04 -0800, Peter Ammon wrote:

....
Yes, the comma operator. But ABS(0, -1) is parsed as attempting to
invoke ABS with two parameters rather than a single comma-parameter, and
ABS((0, -1)) works as expected because then the argument is already
parenthesized.
True, and for the same reason most macros don't bother to parenthesise
macro arguments that are passed on as function/macro arguments in the
macro body, e.g.

#define MYREALLOC(ptr, size) realloc(ptr, size)
(Thinking...)

I can produce an error with the double-macro trick:

#define x 0,-1
#define ABS(a) ((a)<0?-(a):a)

Then ABS(x) is -1.
Good catch, and very sneaky. A macro expanding to an unparenthesised
expression, especially a comma operator is pure evil. If anything is is
the x macro that is broken here, not the ABS() macro.
I'll have to give the book more credit if that's the solution the author
was looking for!


I'm not convinced. It is too obscure to expect somebody just learning the
language to come up with it. IMO either we're missing something or the
case that King was thinking about is wrong.

Anothing thing to consider for completeness is the conditional operator
itself, e.g. ABS(0 ? -1 : -2). However the conditional operator is right
associative so this does the right(!) thing. If it had been left
associative it would have broken King's ABS() macro.

Lawrence
Nov 14 '05 #7
Peter Ammon wrote:
Eric Sosman wrote:
Peter Ammon wrote:
K.N. King's book "C Programming: A Modern Approach" gives this
exercise in chapter 14, which is about the preprocessor:

The following macro has a subtle problem:

#define ABS(a) ((a)<0?-(a):a)

Give an example that shows why ABS doesn't work,
and show how to fix the problem. You may assume
that the argument to ABS doesn't have a side effect.

The only potential problem I see is when INT_MIN is passed, which I
do not believe to be the solution that King has in mind. Can anyone
shed some light on this problem? Most of the other problems in the
chapter are related to not parenthesizing the arguments properly, but
I can't think of a case where the missing parenthesis could causse a
problem in the above macro.
Hint: Can, you, think, of, any, C, operator, without,
side, effects, but, with, lower, prececence, than, `?:'?


(Sorry for the double post, I hit send by mistake)

Yes, the comma operator. But ABS(0, -1) is parsed as attempting to
invoke ABS with two parameters [...]


Hmmm: Right you are. Good catch!
(Thinking...)

I can produce an error with the double-macro trick:

#define x 0,-1
#define ABS(a) ((a)<0?-(a):a)

Then ABS(x) is -1.


Sneaky, evil, admirable, ... and a sign of a twisted mind.

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 14 '05 #8

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

Similar topics

1
by: me | last post by:
Hi guys I want to insert a load of pieces of data into a map The map has an std::string representing a field name as the key, and the value is a struct with 2 members - the field length and a...
33
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
1
by: alon | last post by:
hello, i have a problem , i am a new user at access , i am using access 2000 , when i am making macro with msgbox the access stucks, i have check the same file with the macro on other computer and...
1
by: Patrick_Montana | last post by:
I am having a problem auto-activating a macro when I change data in a field on a form that is a combo box. I have a fair understanding of Microsoft Access but am not a programmer so I am trying to...
6
by: Roman Mashak | last post by:
Hello, All! I would like to use this macro to substitute type of variable, but it doesn't work by now: typedef enum table_type_e { INT, FLOAT, DOUBLE } table_type_t;
3
by: zhangyue.zl | last post by:
Before I thought C is simple and convient , but now I dont think so.There is really some ugly thing in C.Today I see some macro declaration like this: void va_end (va_list); /* Defined in...
1
by: nikhil kumar | last post by:
Hi all i am trying to develop a macro. the scenario is as follows,, we have 2 sheets 1 and 2. i want to apply a vlookup in sheet 1 and the lookup table is in sheet 2. but in sheet 1...
0
by: jhicke03 | last post by:
Hi, This problem has been bugging me for a while. In my form's before update field i have the following code: Private Sub Form_BeforeUpdate(Cancel As Integer) If Me.Nominator_name = "" Or...
3
by: jl2886 | last post by:
I would like a all records from (a table) where .="Rejected" and input those records into the (a table). Both tables are exactly the same as far as structure. I have created a macro with one...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.