Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Const vs # Define in the header file

Reply
Thread Tools

Const vs # Define in the header file

 
 
Rajan
Guest
Posts: n/a
 
      04-06-2005
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

 
Reply With Quote
 
 
 
 
Lionel
Guest
Posts: n/a
 
      04-06-2005
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.
 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      04-06-2005
"Rajan" <> wrote in message
news: 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


 
Reply With Quote
 
ulrich
Guest
Posts: n/a
 
      04-06-2005
On 5 Apr 2005 23:30:41 -0700, Rajan <> 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.
 
Reply With Quote
 
George Faraj
Guest
Posts: n/a
 
      04-06-2005
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" <> wrote in message
news: 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
>



 
Reply With Quote
 
gfaraj@gmail.com
Guest
Posts: n/a
 
      04-06-2005
ulrich, the compiler can optimize and I think it should be the same
memory usage as using a define.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-06-2005
* 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?
 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      04-06-2005
"ulrich" <> wrote in message
newspsoswgqxan2mgp5@innsbruck-neu...
> On 5 Apr 2005 23:30:41 -0700, Rajan <> 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



 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-06-2005
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.

 
Reply With Quote
 
ulrich
Guest
Posts: n/a
 
      04-06-2005
On Wed, 6 Apr 2005 20:05:07 +1000, David White <> wrote:

> "ulrich" <> wrote in message
> newspsoswgqxan2mgp5@innsbruck-neu...
>> On 5 Apr 2005 23:30:41 -0700, Rajan <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
const int versus #define - does "const int" take up space in exe? ThazKool C++ 1 06-11-2006 04:55 AM
use a const variable as opposed to #define - Multiple const objects created yancheng.cheok@gmail.com C++ 4 05-05-2006 10:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57