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

behavior of define

I was read Linux kernel code [linux-2.6.19] in which I saw few define
macro defines in functions. The snap of the function is following and
file name "err_ev6.c".

static int
ev6_parse_cbox(u64 c_addr, u64 c1_syn, u64 c2_syn,
u64 c_stat, u64 c_sts, int print)
{
char *sourcename[] = { "UNKNOWN", "UNKNOWN", "UNKNOWN",
"MEMORY", "BCACHE", "DCACHE",
"BCACHE PROBE", "BCACHE PROBE" };
char *streamname[] = { "D", "I" };
char *bitsname[] = { "SINGLE", "DOUBLE" };
int status = MCHK_DISPOSITION_REPORT;
int source = -1, stream = -1, bits = -1;

#define EV6__C_STAT__BC_PERR (0x01)
#define EV6__C_STAT__DC_PERR (0x02)
#define EV6__C_STAT__DSTREAM_MEM_ERR (0x03)
#define EV6__C_STAT__DSTREAM_BC_ERR (0x04)
..
..
..
}

I was tested following code.

#define AAA 90

void fun( )
{
#define _AAA 100
}

int main()
{
printf( "%d %d", AAA, _AAA );
return 0;
}

The result was "90 100". I am not able to understand, what is the
difference in use of define macro to defines in Global scoop or with in
Function scoop?

Regards,

-aims

Jan 9 '07 #1
4 2403

Mohammad Omer Nasir napsal:
I was read Linux kernel code [linux-2.6.19] in which I saw few define
macro defines in functions. The snap of the function is following and
file name "err_ev6.c".

static int
ev6_parse_cbox(u64 c_addr, u64 c1_syn, u64 c2_syn,
u64 c_stat, u64 c_sts, int print)
{
char *sourcename[] = { "UNKNOWN", "UNKNOWN", "UNKNOWN",
"MEMORY", "BCACHE", "DCACHE",
"BCACHE PROBE", "BCACHE PROBE" };
char *streamname[] = { "D", "I" };
char *bitsname[] = { "SINGLE", "DOUBLE" };
int status = MCHK_DISPOSITION_REPORT;
int source = -1, stream = -1, bits = -1;

#define EV6__C_STAT__BC_PERR (0x01)
#define EV6__C_STAT__DC_PERR (0x02)
#define EV6__C_STAT__DSTREAM_MEM_ERR (0x03)
#define EV6__C_STAT__DSTREAM_BC_ERR (0x04)
.
.
.
}

I was tested following code.

#define AAA 90

void fun( )
{
#define _AAA 100
}

int main()
{
printf( "%d %d", AAA, _AAA );
return 0;
}

The result was "90 100". I am not able to understand, what is the
difference in use of define macro to defines in Global scoop or with in
Function scoop?

Regards,

-aims
It does not matter, where you #define your macro. When something is
defined inside of function, it is usually #undefined in the same
function (I do not know, ehther it is in your case too), because such
macro is meant only for usage in this function.

Jan 9 '07 #2

Mohammad Omer Nasir wrote:
I was read Linux kernel code [linux-2.6.19] in which I saw few define
macro defines in functions. The snap of the function is following and
file name "err_ev6.c".

static int
ev6_parse_cbox(u64 c_addr, u64 c1_syn, u64 c2_syn,
u64 c_stat, u64 c_sts, int print)
{
<snip>
#define EV6__C_STAT__BC_PERR (0x01)
<snip>
}

I was tested following code.

#define AAA 90

void fun( )
{
#define _AAA 100
}

int main()
{
printf( "%d %d", AAA, _AAA );
return 0;
}

The result was "90 100". I am not able to understand, what is the
difference in use of define macro to defines in Global scoop or with in
Function scoop?
You mean scope, not scoop.

What is the difference? Nothing at all. Macro substitution (in your
case, replacing all[*] occurances of AAA with 90 and all[*] occurances
of _AAA with 100) is done as a simple textual replacement by the
preprocessor before the compiler gets to see the code.
[*] When I say all, I really mean all occurances of the symbol that
appear *after* the #define.

The preprocessor knows nothing about C++ scope rules. After it
encounters #define AAA 90, it will blindly replace all subsequent
occurances of AAA with 90, regardless of (amongst other things) the
scope of the #define or the symbol.

I'm only guessing, but one possible reason for the style of the code
you found is that, if the macro EV6__C_STAT__BC_PERR is only intended
to be used within the ev6_parse_cbox function, the author may have put
the macro definition inside the function to document that intent to a
human reader. But the preprocessor doesn't know that and, as you have
found, it will replace all subsequent occurances of
EV6__C_STAT__BC_PERR with (0x01) whether they are inside the definition
of ev6_parse_cbox or not.

Two things to note from a C++ point of view:
1. In C++, their is no reason to use a macro for a simple constant like
this. Use const int instead. Then the compiler will be able to respect
all the C++ rules (e.g. scope, type) that the preprocessor knows
nothing about.
2. Don't use names with a double underscore in your own code. They are
reserved for the implementation.

Gavin Deane

Jan 9 '07 #3
Mohammad Omer Nasir wrote:
{
#define _AAA 100
}
UNDEFINED. Underscore followed by uppercase letter is reserved
to the implementation.
>
int main()
{
printf( "%d %d", AAA, _AAA );
return 0;
}

The result was "90 100". I am not able to understand, what is the
difference in use of define macro to defines in Global scoop or with in
Function scoop?
#define has no scope. It's a text substitution that occurs early
on in the program compilation.

const variables, enums, etc... have scope and should be preferred.
Jan 9 '07 #4
Thanks, now all of the points are clear..

regards,

-aims

On Jan 9, 3:28 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
Mohammad Omer Nasir wrote:
I was read Linux kernel code [linux-2.6.19] in which I saw few define
macro defines in functions. The snap of the function is following and
file name "err_ev6.c".
static int
ev6_parse_cbox(u64 c_addr, u64 c1_syn, u64 c2_syn,
u64 c_stat, u64 c_sts, int print)
{<snip>
#define EV6__C_STAT__BC_PERR (0x01)<snip>


}
I was tested following code.
#define AAA 90
void fun( )
{
#define _AAA 100
}
int main()
{
printf( "%d %d", AAA, _AAA );
return 0;
}
The result was "90 100". I am not able to understand, what is the
difference in use of define macro to defines in Global scoop or with in
Function scoop?You mean scope, not scoop.

What is the difference? Nothing at all. Macro substitution (in your
case, replacing all[*] occurances of AAA with 90 and all[*] occurances
of _AAA with 100) is done as a simple textual replacement by the
preprocessor before the compiler gets to see the code.
[*] When I say all, I really mean all occurances of the symbol that
appear *after* the #define.

The preprocessor knows nothing about C++ scope rules. After it
encounters #define AAA 90, it will blindly replace all subsequent
occurances of AAA with 90, regardless of (amongst other things) the
scope of the #define or the symbol.

I'm only guessing, but one possible reason for the style of the code
you found is that, if the macro EV6__C_STAT__BC_PERR is only intended
to be used within the ev6_parse_cbox function, the author may have put
the macro definition inside the function to document that intent to a
human reader. But the preprocessor doesn't know that and, as you have
found, it will replace all subsequent occurances of
EV6__C_STAT__BC_PERR with (0x01) whether they are inside the definition
of ev6_parse_cbox or not.

Two things to note from a C++ point of view:
1. In C++, their is no reason to use a macro for a simple constant like
this. Use const int instead. Then the compiler will be able to respect
all the C++ rules (e.g. scope, type) that the preprocessor knows
nothing about.
2. Don't use names with a double underscore in your own code. They are
reserved for the implementation.

Gavin Deane
Jan 10 '07 #5

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

Similar topics

3
by: Andrew | last post by:
Hi all , i am supposed to create a program that performs a specific operation to a 2-D matrix whose elements are positive (or zero ). This operation is repeated T times on the matrix and only the...
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
66
by: Mantorok Redgormor | last post by:
#include <stdio.h> struct foo { int example; struct bar *ptr; }; int main(void) { struct foo baz; baz.ptr = NULL; /* Undefined behavior? */ return 0;
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
6
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to...
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
3
by: Mike Cain | last post by:
I have an odd situation I'm trying to understand..... I'm using MS VS 7.0 C++ with the MFC CBuffer class. If I do this: #define NUM_ELEMENTS 2 CBuffer<char, 512 x; CBuffer<char, 512 ...
28
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
2
by: Chris Thomasson | last post by:
I was wondering if the 'SLINK_*' and 'SLIST_*' macros, which implement a simple singly-linked list, will produce _any_ possible undefined behavior: ____________________________ #include...
4
by: er | last post by:
hi, the code below generates the following behavior. cld someone please help understand it? 1) clean project + build project generates build errors (see bottom) 2) build a second time, errors...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.