use a const variable as opposed to #define - Multiple const objects created

Discussion in 'C++' started by yancheng.cheok@gmail.com, May 5, 2006.

  1. Guest

    Recently, I try to replace #define with const in header file. However,
    there are concerns on, multiple const object will be created, if the
    header file is included in multiple cpp files.

    For example:

    In version.h
    ------------
    #ifndef VERSION
    #define VERSION

    #include <string>

    const std::string version("alpha_0-22");

    #endif


    In main.cpp
    ------------
    #include <cstdio>
    #include "version.h"

    extern void fun();

    int main()
    {
    printf("address of version in main=%p\n", &version);
    fun();
    getchar();
    }

    In fun.cpp
    ------------
    #include <cstdio>
    #include "version.h"

    void fun()
    {
    printf("address of version in fun=%p\n", &version);
    }

    The output of the program will be:

    address of version in main=00431960
    address of version in fun=00431984

    It seems that two copies of version string had been created if
    version.h is included in different cpp file scope. Now I am worry if
    version.h file is included in thousand of cpp files, will thousand of
    version string object be created?!

    My alternative workaround on this is, I will let version.h declare the
    version string and version.cpp define the version string.

    In version.h
    ------------
    #ifndef VERSION
    #define VERSION

    #include <string>

    extern const std::string version;

    #endif

    In version.cpp
    --------------
    #include "version.h"

    const std::string version("alpha_0-22");

    Again, here is my output:

    address of version in main=00431960
    address of version in fun=00431960

    It seems that the const string just be constructed one time only.

    I am not sure whether this is the correct workaround? Or my concern on
    multiple creation of const object is not an issues?

    Please refer to
    http://www.parashift.com/c -faq-lite/newbie.html#faq-29.7 on why I am
    using const instead of const.

    Thank you for your feedback.

    yccheok
     
    , May 5, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

    Re: use a const variable as opposed to #define - Multiple const objectscreated

    wrote:
    > Recently, I try to replace #define with const in header file. However,
    > there are concerns on, multiple const object will be created, if the
    > header file is included in multiple cpp files.
    >

    Make them extern const.

    --
    Ian Collins.
     
    Ian Collins, May 5, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    >
    > I am not sure whether this is the correct workaround? Or my concern on
    > multiple creation of const object is not an issues?
    >


    I am not an expert, but if I remember correctly a C++ compiler is
    actually allowed to optimize away the const objects, provided you don't
    do anything fancy to them and/or take their address. Maybe someone can
    clarify on this?
     
    , May 5, 2006
    #3
  4. Ian Collins Guest

    Re: use a const variable as opposed to #define - Multiple const objectscreated

    wrote:
    > wrote:
    >
    >>I am not sure whether this is the correct workaround? Or my concern on
    >>multiple creation of const object is not an issues?
    >>

    >
    >
    > I am not an expert, but if I remember correctly a C++ compiler is
    > actually allowed to optimize away the const objects, provided you don't
    > do anything fancy to them and/or take their address. Maybe someone can
    > clarify on this?
    >

    It most likely will, but perhaps not with a complex object like a string.

    The issue is constants declared in headers have local scope to the
    compilation unit, while those declared 'extern' have global scope and
    must have only one definition.

    --
    Ian Collins.
     
    Ian Collins, May 5, 2006
    #4
  5. Re: use a const variable as opposed to #define - Multiple const objectscreated

    * Ian Collins:
    > wrote:
    >> wrote:
    >>
    >>> I am not sure whether this is the correct workaround? Or my concern on
    >>> multiple creation of const object is not an issues?
    >>>

    >>
    >> I am not an expert, but if I remember correctly a C++ compiler is
    >> actually allowed to optimize away the const objects, provided you don't
    >> do anything fancy to them and/or take their address. Maybe someone can
    >> clarify on this?
    >>

    > It most likely will, but perhaps not with a complex object like a string.
    >
    > The issue is constants declared in headers have local scope to the
    > compilation unit, while those declared 'extern' have global scope and
    > must have only one definition.


    I think that's a good enough explanation for the OP.

    However, for the record I think it should be noted that (1) the C++
    language has no notion of header versus implementation file, so the
    placement of a constant here or there does not affect its linkage, and
    (2) it's possible to declare and define 'extern' constants in header
    files without practical problems, but due to historical reasons (the
    order things were introduced in in the language) one must do that via
    the template mechanism, not just declaring the constants 'inline'.

    --
    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?
     
    Alf P. Steinbach, May 5, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. anon
    Replies:
    5
    Views:
    347
    Keith W. McCammon
    May 19, 2004
  2. JKop
    Replies:
    39
    Views:
    1,290
    Old Wolf
    Jun 27, 2004
  3. jimjim
    Replies:
    12
    Views:
    1,929
    Ron Natalie
    Jun 3, 2005
  4. Peng Yu

    When to use #define and When to use const?

    Peng Yu, Dec 7, 2004, in forum: C Programming
    Replies:
    7
    Views:
    367
    CBFalconer
    Dec 8, 2004
  5. Javier
    Replies:
    2
    Views:
    618
    James Kanze
    Sep 4, 2007
Loading...

Share This Page