Discussion:
C++ Macro vs. Global Constant
(too old to reply)
Evan Brodie
2010-05-22 05:21:24 UTC
Permalink
Hi,

When I was reading the course guidelines for C++ programming, I came
across a section that says: In this course, global macros and types
are allowed, while global variables are strongly discouraged.

As far as I understand, a global macro is written near the beginning
of the file (before our routines) as #define MY_PI = 3.14. This will
serve like a global constant, which could be written as const int
MY_PI = 3.14

Can someone please let me know if my analysis is accurate and please
fill me in on the differences between a macro and a global variable/
constant?

Thanks in advance.
Evan
Roman Lapin
2010-05-22 21:08:45 UTC
Permalink
Post by Evan Brodie
Hi,
When I was reading the course guidelines for C++ programming, I came
across a section that says: In this course, global macros and types
are allowed, while global variables are strongly discouraged.
As far as I understand, a global macro is written near the beginning
of the file (before our routines) as #define MY_PI = 3.14. This will
serve like a global constant, which could be written as const int
MY_PI = 3.14
Can someone please let me know if my analysis is accurate and please
fill me in on the differences between a macro and a global variable/
constant?
Thanks in advance.
Evan
Good to see that someone is actually reading the guidelines, props to you =)
I will need Caroline to clarify whether that applies to 246 since those
guidelines were stolen from a 3xx class and she would know whether it's
relevant for our purposes or not.
Now to answer your question. I'm not a professional when it comes to
optimization but I can think of a very simple reason for you.
When you use a macro, the preprocessor will actually go through the
program and replace all instances of x with the value that you assigned
to it. So it is essentially the same as just hardcoding the value
(except it's much easier to read and change of needed).
When using a constant you don't get any advantage over a macro, the
functionality is essentially the same, but the variable is stored (as
you should see on a2q3) and also each time it is encountered in the
program, extra time is taken to retrieve and use the value. Now I am not
sure whether that will amount to the same thing as using a macro if I
was to use -O2 or -O3 for example, but that's the best I can come up with =)
Caroline Kierstead
2010-05-23 16:29:31 UTC
Permalink
Post by Roman Lapin
Post by Evan Brodie
Hi,
When I was reading the course guidelines for C++ programming, I came
across a section that says: In this course, global macros and types
are allowed, while global variables are strongly discouraged.
As far as I understand, a global macro is written near the beginning
of the file (before our routines) as #define MY_PI = 3.14. This will
serve like a global constant, which could be written as const int
MY_PI = 3.14
Can someone please let me know if my analysis is accurate and please
fill me in on the differences between a macro and a global variable/
constant?
Thanks in advance.
Evan
Good to see that someone is actually reading the guidelines, props to you =)
I will need Caroline to clarify whether that applies to 246 since those
guidelines were stolen from a 3xx class and she would know whether it's
relevant for our purposes or not.
Now to answer your question. I'm not a professional when it comes to
optimization but I can think of a very simple reason for you.
When you use a macro, the preprocessor will actually go through the
program and replace all instances of x with the value that you assigned
to it. So it is essentially the same as just hardcoding the value
(except it's much easier to read and change of needed).
Right. The only other issue is that it doesn't have the type set like it
would with a global constant. So, it's very C-like to use #define to create
constant values. The preferred C++ way is to instead do something like:

cont int ARRAY_SIZE = 10;

So, that's one part of the guidelines I agree with and think you should
follow generally i.e. global types and constants are okay, but global
variables should be avoided.
Post by Roman Lapin
When using a constant you don't get any advantage over a macro, the
functionality is essentially the same, but the variable is stored (as
you should see on a2q3) and also each time it is encountered in the
program, extra time is taken to retrieve and use the value. Now I am not
sure whether that will amount to the same thing as using a macro if I
was to use -O2 or -O3 for example, but that's the best I can come up with =)
It's always preferable to change only one location rather than multiple,
so constants are just plain a good idea in principle for maintenance. I'm
sure one possible compiler optimization would be to replace constants with
values...
--
--
Caroline Kierstead, Undergraduate Operations Coordinator
David R. Cheriton School of Computer Science
University of Waterloo, DC3122 (519) 888-4567 x36226
Continue reading on narkive:
Loading...