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 | | | | re: Strange MACRO Problem
On Mon, 18 Jan 2004, Karim Thapa wrote:[color=blue]
>
> Why following macro does not work?
>
> #define removebrace(x) x[/color]
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'.
[color=blue]
> void Foo(int a, int b, char *txt, int d, int e);
>
> main()
> {
> ..
> Foo(1, 2, removebrace(("hello", 5, 6)) );[/color]
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.
[color=blue]
> I expected following expansion by compiler.
>
> Foo(1, 2, "hello", 5, 6);[/color]
Why on earth would you expect *that*?
[color=blue]
> 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.[/color]
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.
[color=blue]
> However, if I do not use macro 'removebrace'[/color]
[and remove both pairs of the extra parentheses][color=blue]
> , all five arguments get
> pushed to the stack before calling function 'Foo', and it works fine.[/color]
Naturally.
[color=blue]
> Any help, why MACRO removebrace does not work. I use MSVC 6.[/color]
It does work. It replaces removebrace(x) by x.
[color=blue]
> Interestingly, this macro works fine in following statement
>
> p = q * removebrace(a+b)
>
> which results in
>
> p = (q*a) + b[/color]
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 | | | | re: Strange MACRO Problem
"Karim Thapa" <karimthapa@yahoo.com> wrote in message
news:cda8d031.0401182313.42284a28@posting.google.c om...[color=blue]
> 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)) );[/color]
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.
[..][color=blue]
>
> Any help, why MACRO removebrace does not work. I use MSVC 6.
>[/color]
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/ | | | | re: Strange MACRO Problem
In article <bug6hb$h61cl$1@ID-203837.news.uni-berlin.de>,
"Vijay Kumar R Zanvar" <vijoeyz@hotpop.com> wrote:
[color=blue]
> "Karim Thapa" <karimthapa@yahoo.com> wrote in message
> news:cda8d031.0401182313.42284a28@posting.google.c om...[color=green]
> > 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)) );[/color]
>
> 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.
>
> [..][color=green]
> >
> > Any help, why MACRO removebrace does not work. I use MSVC 6.
> >[/color]
>
> 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) );[/color]
Most likely not, because now you are passing three arguments to a macro
that only expects one. | | | | re: Strange MACRO Problem
Karim Thapa wrote:[color=blue]
> 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)) );
> }[/color]
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.
[color=blue]
>
> 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.[/color]
All is as it should be. `x' is ("hello",5,6), not "hello",5,6.
[color=blue]
> Any help, why MACRO removebrace does not work. I use MSVC 6.[/color]
It works as it should.
[color=blue]
> Interestingly, this macro works fine in following statement
>
> p = q * removebrace(a+b)
>
> which results in
>
> p = (q*a) + b[/color]
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 | | | | re: Strange MACRO Problem
[..]
[color=blue][color=green]
> >
> > 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) );[/color]
>
> Most likely not, because now you are passing three arguments to a macro
> that only expects one.[/color]
True. I am committed to do one mistake per posting!! :-))
Thanks.
--
vijay-z | | | | re: Strange MACRO Problem
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" <ajo@nospam.andrew.cmu.edu> wrote in message news:<Pine.LNX.4.58-035.0401190248310.20462@unix42.andrew.cmu.edu>...[color=blue]
> On Mon, 18 Jan 2004, Karim Thapa wrote:[color=green]
> >
> > Why following macro does not work?
> >
> > #define removebrace(x) x[/color]
>
> 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'.
>[color=green]
> > void Foo(int a, int b, char *txt, int d, int e);
> >
> > main()
> > {
> > ..
> > Foo(1, 2, removebrace(("hello", 5, 6)) );[/color]
>
> 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.
>[color=green]
> > I expected following expansion by compiler.
> >
> > Foo(1, 2, "hello", 5, 6);[/color]
>
> Why on earth would you expect *that*?
>[color=green]
> > 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.[/color]
>
> 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.
>[color=green]
> > However, if I do not use macro 'removebrace'[/color]
> [and remove both pairs of the extra parentheses][color=green]
> > , all five arguments get
> > pushed to the stack before calling function 'Foo', and it works fine.[/color]
>
> Naturally.
>[color=green]
> > Any help, why MACRO removebrace does not work. I use MSVC 6.[/color]
>
> It does work. It replaces removebrace(x) by x.
>[color=green]
> > Interestingly, this macro works fine in following statement
> >
> > p = q * removebrace(a+b)
> >
> > which results in
> >
> > p = (q*a) + b[/color]
>
> 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[/color] | | | | re: Strange MACRO Problem
"Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> wrote in message news:<Pine.LNX.4.58-035.0401190248310.20462@unix42.andrew.cmu.edu>...[color=blue]
> On Mon, 18 Jan 2004, Karim Thapa wrote:[/color]
....[color=blue]
> Foo(1, 2, ("hello", 5, 6) );
>
> This doesn't match the prototype, so the compiler should give you
> a diagnostic error message.
>[color=green]
> > I expected following expansion by compiler.
> >
> > Foo(1, 2, "hello", 5, 6);[/color]
>
> Why on earth would you expect *that*?
>[color=green]
> > 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[/color][/color]
^^^^^^^^^^^^^^^^
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".[color=blue][color=green]
> > calling function 'Foo', instead of all 5 arguments.[/color]
>
> 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.
>[color=green]
> > However, if I do not use macro 'removebrace'[/color]
> [and remove both pairs of the extra parentheses][/color]
.... | | | | re: Strange MACRO Problem
Karim Thapa wrote:[color=blue]
> 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 ?[/color]
Because you gave it 3 arguments and it only takes 1:
[...][color=blue][color=green][color=darkred]
>>>#define removebrace(x) x[/color][/color][/color]
You should have gotten a compiler diagnostic. If you did not, turn the
warnings back on.
--
Martin Ambuhl | | | | re: Strange MACRO Problem
Vijay Kumar R Zanvar wrote:
[color=blue][color=green]
>>#define removebrace(x) x[/color][/color]
[color=blue]
> Your problem solves if you replace
>
> Foo(1, 2, removebrace(("hello", 5, 6)) );
> with
> Foo(1, 2, removebrace("hello", 5, 6) );[/color]
Since when does supplying 3 arguments to a macro taking only one solve
anyone's problems?
--
Martin Ambuhl | | | | re: Strange MACRO Problem
> Most likely not, because now you are passing three arguments to a macro[color=blue]
> that only expects one.[/color]
God! While doing something complex, I missed this silly point.
I have new problem now. I will post a new thread.
Thanks - Karim |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|