By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

Const vs # Define in the header file

P: n/a
Hi All C++ Experts

(1)I want have a simple suggestion from u all experts
which is preferable const or #define and in which cases

(2)in my project i want to avoid hardcoding , for that
i have defined macros in my header files. but we have coding
rules that says "use const for avoiding hardcoding".My question is
that "what will be the difference in terms of memory for Const vs #
Define"

Thanks and regards
Raj

Jul 23 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Rajan wrote:
Hi All C++ Experts

(1)I want have a simple suggestion from u all experts
which is preferable const or #define and in which cases

(2)in my project i want to avoid hardcoding , for that
i have defined macros in my header files. but we have coding
rules that says "use const for avoiding hardcoding".My question is
that "what will be the difference in terms of memory for Const vs #
Define"


I will suggest that const is better when using debuggers because you can
see the value of of consts where as I believe this is not the case for
#define.
Jul 23 '05 #2

P: n/a
"Rajan" <Sh**************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hi All C++ Experts

(1)I want have a simple suggestion from u all experts
which is preferable const or #define and in which cases
const. All cases.
(2)in my project i want to avoid hardcoding , for that
i have defined macros in my header files. but we have coding
rules that says "use const for avoiding hardcoding".My question is
that "what will be the difference in terms of memory for Const vs #
Define"


That's up to the compiler, but I can't see any reason why a good compiler
should do const values any less efficiently than #define.

#define is horrible. It's processed by the pre-processor, not by the C++
compiler proper. The preprocessor does nothing but text replacement, so
#defines have no respect for the usual C++ scoping rules. They are ghastly,
and I can't think of a single reason why you'd use a #define where a const
can be used.

DW
Jul 23 '05 #3

P: n/a
On 5 Apr 2005 23:30:41 -0700, Rajan <Sh**************@gmail.com> wrote:

[...]
(1)I want have a simple suggestion from u all experts
which is preferable const or #define and in which cases
modern c++ style never uses #define for constant values used in c++ code
(this is what all the gurus say, afaik: sutter, myers, alexandrescu, ...)
there may be _very_ rare exceptions.

(2)in my project i want to avoid hardcoding , for that
i have defined macros in my header files. but we have coding
rules that says "use const for avoiding hardcoding".My question is
that "what will be the difference in terms of memory for Const vs #
Define"


well, if you have
const int A = 10;
it will occupy sizeof(int) bytes of memory as long as it lives.

on the other hand, if you have
#define A 10
the _preprocessor_ will literally replace every "A" in your source code by
"10", so this #defined a uses no memory. however, those variables holding
the value A use memory, off course.

remark:
to really avaoid hardcoding of constants (my point of view is that
"hardcoded" is anything the change of which requires a re-compilation),
your program should use an initialisation file. then, the name of this
file is the only thing which needs to be hardcoded.

or even better: pass that name as (the only) command line parameter, or
read it from the registry. then your program will be totally free of
hardcoding.
Jul 23 '05 #4

P: n/a
Always use const, among various reasons, it has scope and has a type, while
a #define has none of those.

In terms of memory, it should be the same thing, I would assume.

Hope that helps,
George Faraj
"Rajan" <Sh**************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hi All C++ Experts

(1)I want have a simple suggestion from u all experts
which is preferable const or #define and in which cases

(2)in my project i want to avoid hardcoding , for that
i have defined macros in my header files. but we have coding
rules that says "use const for avoiding hardcoding".My question is
that "what will be the difference in terms of memory for Const vs #
Define"

Thanks and regards
Raj

Jul 23 '05 #5

P: n/a
ulrich, the compiler can optimize and I think it should be the same
memory usage as using a define.

Jul 23 '05 #6

P: n/a
* Rajan:

(1)[...] which is preferable const or #define and in which cases
'const' is used to define typed constants.

'#define' is used to define macros.
(2)in my project i want to avoid hardcoding , for that
i have defined macros in my header files.
"hardcoding" refers to maintainability, which is a good idea to focus on.

Macros generally lower maintainability.

What you're doing does not make sense in terms of the stated goal.

but we have coding
rules that says "use const for avoiding hardcoding".My question is
that "what will be the difference in terms of memory for Const vs #
Define"


"memory usage" refers to some kind of misguided premature optimization, which
is a really bad idea to focus on.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #7

P: n/a
"ulrich" <ua********@aon.at> wrote in message
news:opsoswgqxan2mgp5@innsbruck-neu...
On 5 Apr 2005 23:30:41 -0700, Rajan <Sh**************@gmail.com> wrote:
well, if you have
const int A = 10;
it will occupy sizeof(int) bytes of memory as long as it lives.
I doubt it, at least for processors that can more efficiently embed
hard-wired immediate values in code than fetch a value from memory.
Examples:
------------
#define A 10

void f(int);

void g()
{
f(A);
}

x86 code generated for g() by VC++ 6.0:
push 10
call f
add esp, 4
------------
const int A = 10;

void f(int);

void g()
{
f(A);
}

x86 code generated for g() by VC++ 6.0:
push 10
call f
add esp, 4
------------
on the other hand, if you have
#define A 10
the _preprocessor_ will literally replace every "A" in your source code by
"10", so this #defined a uses no memory. however, those variables holding
the value A use memory, off course.

remark:
to really avaoid hardcoding of constants (my point of view is that
"hardcoded" is anything the change of which requires a re-compilation),
your program should use an initialisation file. then, the name of this
file is the only thing which needs to be hardcoded.

or even better: pass that name as (the only) command line parameter, or
read it from the registry. then your program will be totally free of
hardcoding.


DW

Jul 23 '05 #8

P: n/a
ulrich wrote:
well, if you have
const int A = 10;
it will occupy sizeof(int) bytes of memory as long as it lives.
It might, or it might not. Depends on the compiler.
on the other hand, if you have
#define A 10
the _preprocessor_ will literally replace every "A" in your source code by
"10", so this #defined a uses no memory.
Might also be just the other way round.
however, those variables holding the value A use memory, off course.
Well, it depends. Some CPUs will insert the value directly into the code
that loads that value into the register. If it has to be loaded from
memory, the memory address needs to be provided in the code instead.
OTOH, some CPUs cannot directly write the value 10 into a register. They
need to read it from some memory location, in which case the #define might
result in an extra memory use for every occurance of A, while the const
would need only one.
Of course that all depends on the capabilities of the compiler and on the
platform. But even if you have the case of the #define needing no memory
and the const needing it, then sizeof(int) isn't a big deal unless you're
on a very limited embedded platform. E.g. on a 32bit platform, sizeof(int)
is usually 4. With 512MB of memory, 4 bytes are about 7e-7 percent of the
total memory.
remark:
to really avaoid hardcoding of constants (my point of view is that
"hardcoded" is anything the change of which requires a re-compilation),
Though I agree here, it seems the OP rather meant avoiding magic numbers
within the code and replacing them with a constant or a #define to
centralize it and to document its meaning through giving it a proper name.
your program should use an initialisation file. then, the name of this
file is the only thing which needs to be hardcoded.


That depends on the purpose of the value. For example, there is no need to
have the following replaced by a configuration file entry:

const int hours_per_day = 24;

because the number of hours per day is not expected to change, and it would
clutter the config file with useless options.

Jul 23 '05 #9

P: n/a
On Wed, 6 Apr 2005 20:05:07 +1000, David White <no@email.provided> wrote:
"ulrich" <ua********@aon.at> wrote in message
news:opsoswgqxan2mgp5@innsbruck-neu...
On 5 Apr 2005 23:30:41 -0700, Rajan <Sh**************@gmail.com> wrote:
well, if you have
const int A = 10;
it will occupy sizeof(int) bytes of memory as long as it lives.


I doubt it, at least for processors that can more efficiently embed
hard-wired immediate values in code than fetch a value from memory.
Examples:
------------
#define A 10

void f(int);

void g()
{
f(A);
}

x86 code generated for g() by VC++ 6.0:
push 10
call f
add esp, 4
------------
const int A = 10;

void f(int);

void g()
{
f(A);
}

x86 code generated for g() by VC++ 6.0:
push 10
call f
add esp, 4
------------


ok, however this translation of the compiler is not c++ business ;-)
Jul 23 '05 #10

P: n/a

"Rolf Magnus" <ra******@t-online.de> schrieb im Newsbeitrag
news:d3*************@news.t-online.com...
[SNIP]
That depends on the purpose of the value. For example, there is no need to
have the following replaced by a configuration file entry:

const int hours_per_day = 24;

because the number of hours per day is not expected to change, and it would clutter the config file with useless options.


....and will certainly lead to funny behavior one day. Imagine a user
changing the hours_per_day value to 20 ;-) IMO there are some things that
should be kept away from the user's (probably subconscious) fiddling.

Cheers
Chris
Jul 23 '05 #11

P: n/a

"Rajan" <Sh**************@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hi All C++ Experts

(1)I want have a simple suggestion from u all experts
which is preferable const or #define and in which cases

This issue is covered by the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-29.7
Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 23 '05 #12

P: n/a
Sumit Rajan wrote:
Rajan wrote:
(1)I want have a simple suggestion from all of you experts.
Which is preferable? const? Or #define?


This issue is covered by the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-29.7


I think that this FAQ covers Rajan's question adequately.
Jul 23 '05 #13

P: n/a

"ulrich" <ua********@aon.at> skrev i en meddelelse
news:opsos6a0drn2mgp5@innsbruck-neu...
On Wed, 6 Apr 2005 20:05:07 +1000, David White <no@email.provided> wrote:
"ulrich" <ua********@aon.at> wrote in message
news:opsoswgqxan2mgp5@innsbruck-neu...
On 5 Apr 2005 23:30:41 -0700, Rajan <Sh**************@gmail.com> wrote:
well, if you have
const int A = 10;
it will occupy sizeof(int) bytes of memory as long as it lives.


I doubt it, at least for processors that can more efficiently embed
hard-wired immediate values in code than fetch a value from memory.
Examples:
------------
#define A 10

void f(int);

void g()
{
f(A);
}

x86 code generated for g() by VC++ 6.0:
push 10
call f
add esp, 4
------------
const int A = 10;

void f(int);

void g()
{
f(A);
}

x86 code generated for g() by VC++ 6.0:
push 10
call f
add esp, 4
------------


ok, however this translation of the compiler is not c++ business ;-)


What do you mean by that? This translation is perfectly legal C++.

/Peter
Jul 23 '05 #14

P: n/a
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:at*********************@news000.worldonline.d k...

"ulrich" <ua********@aon.at> skrev i en meddelelse
news:opsos6a0drn2mgp5@innsbruck-neu...
On Wed, 6 Apr 2005 20:05:07 +1000, David White <no@email.provided> wrote:
x86 code generated for g() by VC++ 6.0:
push 10
call f
add esp, 4
------------
const int A = 10;

void f(int);

void g()
{
f(A);
}

x86 code generated for g() by VC++ 6.0:
push 10
call f
add esp, 4
------------


ok, however this translation of the compiler is not c++ business ;-)


What do you mean by that? This translation is perfectly legal C++.


The source code is perfectly legal C++, but the translation to assembly
isn't. However, if the assembly isn't C++ business, neither was the
statement that a const int occupies szieof(int) bytes of memory, because
that's as compiler-specific as the assembly, and that was what I was
challenging.

DW


Jul 23 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.