There is a problem when using macros. Let assume that some macro is defined

#define SomeMacro( memvar1 ) \
{ \
int *memvar2 = NULL; \
... \
memvar2 = memvar1; \
SomeFunction( memvar2 ); \
}

Someone may call the macro specifying as an argument a variable with the
name memvar2. In this case instead of

memvar2 = memvar1; /* from the macro definition */

we will get

memvar2 = memvar2;

It is not what we wanted to get.

At first I decided to use a special prefix for local macro variable names
such as m_. For example m_memvar2. Well, the code works. However one macro
may call another macro and the same problem can arise.
Is any trick in C which allows to escape the problem?

Vladimir Grigoriev

Re: About duplicate variable names in macros. by Igor

Igor
Wed Oct 08 14:10:08 CDT 2008

Vladimir Grigoriev <vlad.moscow@mail.ru> wrote:
> There is a problem when using macros. Let assume that some macro is
> defined
> #define SomeMacro( memvar1 ) \
> { \
> int *memvar2 = NULL; \
> ... \
> memvar2 = memvar1; \
> SomeFunction( memvar2 ); \
> }
>
> Someone may call the macro specifying as an argument a variable with
> the name memvar2. In this case instead of
>
> memvar2 = memvar1; /* from the macro definition */
>
> we will get
>
> memvar2 = memvar2;
>
> It is not what we wanted to get.

Well, one obvious solution is to use an inline function instead of a
macro.

Otherwise, just give the local variable a long ugly name, e.g.
local_variable_inside_SomeMacro
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: About duplicate variable names in macros. by Norman

Norman
Wed Oct 08 19:58:30 CDT 2008

Igor Tandetnik wrote:
> Vladimir Grigoriev <vlad.moscow@mail.ru> wrote:
>
>>There is a problem when using macros. Let assume that some macro is
>>defined
>>#define SomeMacro( memvar1 ) \
>>{ \
>> int *memvar2 = NULL; \
>> ... \
>> memvar2 = memvar1; \
>> SomeFunction( memvar2 ); \
>>}
>>
>>Someone may call the macro specifying as an argument a variable with
>>the name memvar2. In this case instead of
>>
>>memvar2 = memvar1; /* from the macro definition */
>>
>>we will get
>>
>>memvar2 = memvar2;
>>
>>It is not what we wanted to get.
>
>
> Well, one obvious solution is to use an inline function instead of a
> macro.
>
> Otherwise, just give the local variable a long ugly name, e.g.
> local_variable_inside_SomeMacro
Or a very short name that you'll never use because all of your real
variable names are completely descriptive of what the variable represents.

--
Norm

To reply, change domain to an adult feline.


Re: About duplicate variable names in macros. by Cezary

Cezary
Thu Oct 09 07:57:39 CDT 2008

Hello,

> At first I decided to use a special prefix for local macro variable
> names such as m_. For example m_memvar2. Well, the code works.
> However one macro may call another macro and the same problem can
> arise. Is any trick in C which allows to escape the problem?

Append macro name to the prefix: ,,m_SomeMacro_memvar'',
,,m_OtherMacro_memvar'', and so on. There is no easy way to make the
preprocessor generating unique identifiers.

-- best regards

Cezary Noweta

Re: About duplicate variable names in macros. by Ben

Ben
Thu Oct 09 08:44:50 CDT 2008

Cezary H. Noweta wrote:
> Hello,
>
>> At first I decided to use a special prefix for local macro variable
>> names such as m_. For example m_memvar2. Well, the code works.
>> However one macro may call another macro and the same problem can
>> arise. Is any trick in C which allows to escape the problem?
>
> Append macro name to the prefix: ,,m_SomeMacro_memvar'',
> ,,m_OtherMacro_memvar'', and so on. There is no easy way to make the
> preprocessor generating unique identifiers.

append __LINE__ with the ## concatenator operator?

>
> -- best regards
>
> Cezary Noweta



Re: About duplicate variable names in macros. by Vladimir

Vladimir
Thu Oct 09 10:58:16 CDT 2008


"Ben Voigt [C++ MVP]" <rbv@nospam.nospam> wrote in message
news:enj70UhKJHA.1556@TK2MSFTNGP03.phx.gbl...
>
> append __LINE__ with the ## concatenator operator?
>
>>
It is very interesting. However it looks too artificially.:)

Vladimir Grigoriev



Re: About duplicate variable names in macros. by Cezary

Cezary
Thu Oct 09 12:26:06 CDT 2008

Hello,

Ben Voigt [C++ MVP] wrote:
> append __LINE__ with the ## concatenator operator?

Yes, but under certain circumstances it can lead to ,,compiler limit :
token overflowed internal buffer'' especially when macros will produce
id2234234234... on 234th line ;)

-- best regards

Cezary Noweta

Re: About duplicate variable names in macros. by Cezary

Cezary
Thu Oct 09 12:39:56 CDT 2008

Hello,

Ben Voigt [C++ MVP] wrote:
> append __LINE__ with the ## concatenator operator?

I considered __LINE__. but Vladimir wrote about macros ,,called'' from
other macros. Macro expansion occurs in place of reference, not in place
of definition (#define), so:

#define SomeMacro( memvar1 )
{
int *memvar2 ## __LINE__ = NULL;
memvar2 ## __LINE__ = memvar1;
SomeFunction( memvar2 ## __LINE__ );
}

#define SomeMacro2( memvar1 )
{
int *memvar2 ## __LINE__ = NULL;
memvar2 ## __LINE__ = memvar1;
SomeMacro( memvar2 ## __LINE__ );
}

does not solve the problem, as with, for example:

SomeMacro2(var);

on 190th line, we will have ,,memvar2190 = memvar2190;'' after
SomeMacro() has been expanded.

-- best regards

Cezary Noweta