473,383 Members | 1,929 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,383 software developers and data experts.

Strange MACRO Problem

Why following macro does not work?

#define removebrace(x) x

void Foo(int a, int b, char *txt, int d, int e);

main()
{
...
Foo(1, 2, removebrace(("hello", 5, 6)) );

...
}

I expected following expansion by compiler.

Foo(1, 2, "hello", 5, 6);

But it didnt work. When I see disassembly, only 3 arguments are pushed
into the stack, pointer to string "hello", value 2 and value 1 before
calling function 'Foo', instead of all 5 arguments.

However, if I do not use macro 'removebrace', all five arguments get
pushed to the stack before calling function 'Foo', and it works fine.

Any help, why MACRO removebrace does not work. I use MSVC 6.

Interestingly, this macro works fine in following statement

p = q * removebrace(a+b)

which results in

p = (q*a) + b

Karim

My previous post appeared to be lost so posting it again. Excuse me if
you receive two copy
Nov 14 '05 #1
10 2390

On Mon, 18 Jan 2004, Karim Thapa wrote:

Why following macro does not work?

#define removebrace(x) x
In other words, 'removebrace' is almost -- but not quite -- a no-op.
In particular, assuming 'foo' is not a #defined identifier,

removebrace(foo) ==> foo

for all 'foo'.
void Foo(int a, int b, char *txt, int d, int e);

main()
{
..
Foo(1, 2, removebrace(("hello", 5, 6)) );
Since removebrace(("hello", 5, 6)) expands to ("hello, 5, 6), this
whole line expands to

Foo(1, 2, ("hello", 5, 6) );

This doesn't match the prototype, so the compiler should give you
a diagnostic error message.
I expected following expansion by compiler.

Foo(1, 2, "hello", 5, 6);
Why on earth would you expect *that*?
But it didnt work. When I see disassembly, only 3 arguments are pushed
into the stack, pointer to string "hello", value 2 and value 1 before
calling function 'Foo', instead of all 5 arguments.
That's because your compiler has adequate constant-expression
optimization. It knows that since "hello" and 5 have no side-effects,
they can be simply discarded.
However, if I do not use macro 'removebrace' [and remove both pairs of the extra parentheses] , all five arguments get
pushed to the stack before calling function 'Foo', and it works fine.
Naturally.
Any help, why MACRO removebrace does not work. I use MSVC 6.
It does work. It replaces removebrace(x) by x.
Interestingly, this macro works fine in following statement

p = q * removebrace(a+b)

which results in

p = (q*a) + b


No, it results in

p = q * a+b

which has the same effect; but you've inserted a pair of
parentheses where they don't belong.

There is no C macro 'foo' such that 'foo((bar))' replaces to 'bar'
in general for all text 'bar', if that's what you're looking for.
What are you *really* trying to do, and why?

-Arthur
Nov 14 '05 #2

"Karim Thapa" <ka********@yahoo.com> wrote in message
news:cd**************************@posting.google.c om...
Why following macro does not work?

#define removebrace(x) x

void Foo(int a, int b, char *txt, int d, int e);

main()
{
..
Foo(1, 2, removebrace(("hello", 5, 6)) );
This is equivalent to:

Foo ( 1,2, ("hello", 5, 6 ) );

which in turn is equivalent to:

Foo (1, 2, 6 );

Since the signature of Foo says expect 5 arguments, this one
will generate an error. Know/read about comma operator.

[..]
Any help, why MACRO removebrace does not work. I use MSVC 6.


This hasn't got anyting to do with MSVC 6.

Your problem solves if you replace

Foo(1, 2, removebrace(("hello", 5, 6)) );

with

Foo(1, 2, removebrace("hello", 5, 6) );

[..]

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Nov 14 '05 #3
In article <bu************@ID-203837.news.uni-berlin.de>,
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote:
"Karim Thapa" <ka********@yahoo.com> wrote in message
news:cd**************************@posting.google.c om...
Why following macro does not work?

#define removebrace(x) x

void Foo(int a, int b, char *txt, int d, int e);

main()
{
..
Foo(1, 2, removebrace(("hello", 5, 6)) );


This is equivalent to:

Foo ( 1,2, ("hello", 5, 6 ) );

which in turn is equivalent to:

Foo (1, 2, 6 );

Since the signature of Foo says expect 5 arguments, this one
will generate an error. Know/read about comma operator.

[..]

Any help, why MACRO removebrace does not work. I use MSVC 6.


This hasn't got anyting to do with MSVC 6.

Your problem solves if you replace

Foo(1, 2, removebrace(("hello", 5, 6)) );

with

Foo(1, 2, removebrace("hello", 5, 6) );


Most likely not, because now you are passing three arguments to a macro
that only expects one.
Nov 14 '05 #4
Karim Thapa wrote:
Why following macro does not work?

#define removebrace(x) x

void Foo(int a, int b, char *txt, int d, int e);

main()
{
Foo(1, 2, removebrace(("hello", 5, 6)) );
}
This snippet, BTW, is self-contradictory. Using implict int for the
retutn type of main is OK in C89 but not C99; leaving off the return value
is OK in C99 (where there is an implicit "return 0;") but not in C89.

I expected following expansion by compiler.

Foo(1, 2, "hello", 5, 6);

But it didnt work. When I see disassembly, only 3 arguments are pushed
into the stack, pointer to string "hello", value 2 and value 1 before
calling function 'Foo', instead of all 5 arguments.
All is as it should be. `x' is ("hello",5,6), not "hello",5,6.
Any help, why MACRO removebrace does not work. I use MSVC 6.
It works as it should.
Interestingly, this macro works fine in following statement

p = q * removebrace(a+b)

which results in

p = (q*a) + b


No braces were there to be removed by removebace in your example above.
The parallel case is:
#define removebrace(x) x
int main(void)
{
int a = 1, b = 2, c = 3;
c = c * removebrace((a + b));
return 0;
}

expanding to:

int main()
{
int a = 1, b = 2, c = 3;
c = c * (a + b);
return 0;
}


--
Martin Ambuhl
Nov 14 '05 #5
[..]

This hasn't got anyting to do with MSVC 6.

Your problem solves if you replace

Foo(1, 2, removebrace(("hello", 5, 6)) );

with

Foo(1, 2, removebrace("hello", 5, 6) );


Most likely not, because now you are passing three arguments to a macro
that only expects one.


True. I am committed to do one mistake per posting!! :-))

Thanks.

--
vijay-z
Nov 14 '05 #6
Sorry, I made mistake while posting, it is actually

Foo(1, 2, removebrace("hello", 5, 6) );
Can some one explain now why macro removebrace does not
work as expected ?

I expected following expansion by compiler.

Foo(1, 2, "hello", 5, 6);

But it didnt work. When I see disassembly, only 3 arguments are pushed
into the stack, pointer to string "hello", value 1 and 2 before
calling function 'Foo', instead of all 5 arguments.

However, if I do not use macro 'removebrace', all five arguments get
pushed to the stack before calling function 'Foo'

Any help, why MACRO removebrace does not work. I use MSVC 6.

Karim

"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi***********************************@unix42 .andrew.cmu.edu>...
On Mon, 18 Jan 2004, Karim Thapa wrote:

Why following macro does not work?

#define removebrace(x) x


In other words, 'removebrace' is almost -- but not quite -- a no-op.
In particular, assuming 'foo' is not a #defined identifier,

removebrace(foo) ==> foo

for all 'foo'.
void Foo(int a, int b, char *txt, int d, int e);

main()
{
..
Foo(1, 2, removebrace(("hello", 5, 6)) );


Since removebrace(("hello", 5, 6)) expands to ("hello, 5, 6), this
whole line expands to

Foo(1, 2, ("hello", 5, 6) );

This doesn't match the prototype, so the compiler should give you
a diagnostic error message.
I expected following expansion by compiler.

Foo(1, 2, "hello", 5, 6);


Why on earth would you expect *that*?
But it didnt work. When I see disassembly, only 3 arguments are pushed
into the stack, pointer to string "hello", value 2 and value 1 before
calling function 'Foo', instead of all 5 arguments.


That's because your compiler has adequate constant-expression
optimization. It knows that since "hello" and 5 have no side-effects,
they can be simply discarded.
However, if I do not use macro 'removebrace'

[and remove both pairs of the extra parentheses]
, all five arguments get
pushed to the stack before calling function 'Foo', and it works fine.


Naturally.
Any help, why MACRO removebrace does not work. I use MSVC 6.


It does work. It replaces removebrace(x) by x.
Interestingly, this macro works fine in following statement

p = q * removebrace(a+b)

which results in

p = (q*a) + b


No, it results in

p = q * a+b

which has the same effect; but you've inserted a pair of
parentheses where they don't belong.

There is no C macro 'foo' such that 'foo((bar))' replaces to 'bar'
in general for all text 'bar', if that's what you're looking for.
What are you *really* trying to do, and why?

-Arthur

Nov 14 '05 #7
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi***********************************@unix42 .andrew.cmu.edu>...
On Mon, 18 Jan 2004, Karim Thapa wrote: .... Foo(1, 2, ("hello", 5, 6) );

This doesn't match the prototype, so the compiler should give you
a diagnostic error message.
I expected following expansion by compiler.

Foo(1, 2, "hello", 5, 6);


Why on earth would you expect *that*?
But it didnt work. When I see disassembly, only 3 arguments are pushed
into the stack, pointer to string "hello", value 2 and value 1 before ^^^^^^^^^^^^^^^^
it is strange,here.
the value of the expression ("hello",5,6) is 6,not "hello"??
i think value 6 would be pushed,not "hello". calling function 'Foo', instead of all 5 arguments.


That's because your compiler has adequate constant-expression
optimization. It knows that since "hello" and 5 have no side-effects,
they can be simply discarded.
However, if I do not use macro 'removebrace'

[and remove both pairs of the extra parentheses]

....
Nov 14 '05 #8
Karim Thapa wrote:
Sorry, I made mistake while posting, it is actually

Foo(1, 2, removebrace("hello", 5, 6) );
Can some one explain now why macro removebrace does not
work as expected ?


Because you gave it 3 arguments and it only takes 1:
[...]
#define removebrace(x) x


You should have gotten a compiler diagnostic. If you did not, turn the
warnings back on.
--
Martin Ambuhl
Nov 14 '05 #9
Vijay Kumar R Zanvar wrote:

#define removebrace(x) x
Your problem solves if you replace

Foo(1, 2, removebrace(("hello", 5, 6)) );
with
Foo(1, 2, removebrace("hello", 5, 6) );


Since when does supplying 3 arguments to a macro taking only one solve
anyone's problems?
--
Martin Ambuhl
Nov 14 '05 #10
> Most likely not, because now you are passing three arguments to a macro
that only expects one.


God! While doing something complex, I missed this silly point.

I have new problem now. I will post a new thread.

Thanks - Karim
Nov 14 '05 #11

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

Similar topics

8
by: Dave | last post by:
Hello all, Here is the definition I'm using of a static const class member: template<typename T, typename U> const U directed_graph_t<T, U>::INFINITY = ( std::numeric_limits<U>::has_infinity...
2
by: Tony Ciconte | last post by:
We have developed an Acc97 application and distribute it using the Wise installation system and SageKey scripts. The installations are rock solid and the product works well on all types of systems....
26
by: GS | last post by:
I am doing my first real embedded programming project and the supplied device libraries have a function definition that looks like this: void FCN(void) = { INTDEF, INTABS, (unsigned short)...
1
by: Shrishail Rana | last post by:
Hello all, Here's another mind puzzle from me :-) Today I tried to add a new resource to my program and VS.NET (v 7.0.9466) just died. What I mean by that is that it silently and quickly...
5
by: cmiddlebrook | last post by:
Greetings all, I have a class which contains a function with the following signature: MC_SDLImage* LoadImage(const std::string& file, int id); and I call this function in two places in my...
11
by: vj | last post by:
Hello group, I am working on a compression tool and saw this puzzling bit shit behaviour on a VC++6.0 compiler. #include <iostream> using namespace std; typedef unsigned char uchar; #define...
9
by: Me | last post by:
Hi, I ran into a malloc problem but I can't find the solution. I try to read a file into a variable with malloc like this: BYTE *lcdata; lcdata = malloc(fsize*sizeof(BYTE));
0
by: mmcd79 | last post by:
I'm wondering if anyone else is having this issue. It's driving me batty. It seems my mouse wants to "double click" on items when I'm just doing a single click. I thought it was the mouse so I...
20
by: dis_is_eagle | last post by:
Hi.I could not understand the output of the following program. #define swap(a,b) temp=a; a=b; b=temp; int main( ) { int i, j, temp; i=5; j=10; temp=0; if( i j)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.