Const vs # Define in the header file

R

Rajan

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
 
L

Lionel

Rajan said:
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.
 
D

David White

Rajan said:
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
 
U

ulrich

[...]
(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.
 
G

George Faraj

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
 
G

gfaraj

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

Alf P. Steinbach

* 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.
 
D

David White

ulrich said:
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
 
R

Rolf Magnus

ulrich said:
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.
 
U

ulrich

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 ;-)
 
C

Chris Theis

[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
 
P

Peter Koch Larsen

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

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

/Peter
 
D

David White

Peter Koch Larsen said:
ulrich said:
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
 
U

ulrich

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

sure. just wanted to say that imho the c++ standard does not _prescribe_
the sequence of machine commands which should result from any given piece
of c++ source, afaik.
 
U

ulrich

[...]
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.

well, yes, you're right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top