'const' saves space??

Discussion in 'C++' started by qazmlp, Aug 9, 2003.

  1. qazmlp

    qazmlp Guest

    As I understand, there will be 100 copies of "myTestClass", when
    CLASSNAME is declared as below:
    #define CLASSNAME "myTestClass"

    But, there will be only 1 copy of "myTestClass", when
    CLASSNAME is declared as below:
    const char* CLASSNAME = "myTestClass" ;

    Is my understanding correct ? Isn't that an advantage of 'const'
    over '#define' declaration for constants?
    qazmlp, Aug 9, 2003
    #1
    1. Advertising

  2. On 9 Aug 2003 03:21:14 -0700
    (qazmlp) wrote:
    > As I understand, there will be 100 copies of "myTestClass", when
    > CLASSNAME is declared as below:
    > #define CLASSNAME "myTestClass"


    No. "myTestClass" will exist once, and all literals will be translated to a
    pointer to that address.

    printf ("%d\n", "foo" - "foo");
    will print 0.

    --
    char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
    c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
    c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
    Pieter Droogendijk, Aug 9, 2003
    #2
    1. Advertising

  3. "Pieter Droogendijk" <> wrote in message
    news:...
    > On 9 Aug 2003 03:21:14 -0700
    > (qazmlp) wrote:
    > > As I understand, there will be 100 copies of "myTestClass", when
    > > CLASSNAME is declared as below:
    > > #define CLASSNAME "myTestClass"

    >
    > No. "myTestClass" will exist once, and all literals will be translated to

    a
    > pointer to that address.
    >
    > printf ("%d\n", "foo" - "foo");
    > will print 0.



    Will the Standard ensure that this is 0 or does it just be 0 on some
    implementation(s)?

    regards
    --
    jb

    (replace y with x if you want to reply by e-mail)
    Jakob Bieling, Aug 9, 2003
    #3
  4. On Sat, 09 Aug 2003 12:52:16 +0200, Pieter Droogendijk wrote:

    > On 9 Aug 2003 03:21:14 -0700
    > (qazmlp) wrote:
    >> As I understand, there will be 100 copies of "myTestClass", when
    >> CLASSNAME is declared as below:
    >> #define CLASSNAME "myTestClass"

    >
    > No. "myTestClass" will exist once, and all literals will be translated to a
    > pointer to that address.
    >
    > printf ("%d\n", "foo" - "foo");
    > will print 0.


    Not necessarily. Some compilers do this by default, others support it as
    an optional feature, while others don't support it at all.
    Simon Saunders, Aug 9, 2003
    #4
  5. On Sat, 09 Aug 2003 12:21:29 +0100
    "Simon Saunders" <> wrote:
    > On Sat, 09 Aug 2003 12:52:16 +0200, Pieter Droogendijk wrote:
    >
    > > On 9 Aug 2003 03:21:14 -0700
    > > (qazmlp) wrote:
    > >> As I understand, there will be 100 copies of "myTestClass", when
    > >> CLASSNAME is declared as below:
    > >> #define CLASSNAME "myTestClass"

    > >
    > > No. "myTestClass" will exist once, and all literals will be translated to a
    > > pointer to that address.
    > >
    > > printf ("%d\n", "foo" - "foo");
    > > will print 0.

    >
    > Not necessarily. Some compilers do this by default, others support it as
    > an optional feature, while others don't support it at all.


    I see. Thanks. I guess this is OT after all...

    --
    char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
    c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
    c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
    Pieter Droogendijk, Aug 9, 2003
    #5
  6. qazmlp wrote:

    > As I understand, there will be 100 copies of "myTestClass"
    > when CLASSNAME is declared as below:


    > #define CLASSNAME "myTestClass"
    >
    > But there will be only 1 copy of "myTestClass"
    > when CLASSNAME is declared as below:


    > const char* CLASSNAME = "myTestClass" ;
    >
    > Is my understanding correct? Isn't that an advantage of 'const'
    > over '#define' declaration for constants?



    GCC(1) GNU GCC(1)

    NAME
    gcc - GNU project C and C++ compiler
    E. Robert Tisdale, Aug 9, 2003
    #6
  7. qazmlp

    Dan Pop Guest

    In <> (qazmlp) writes:

    >As I understand, there will be 100 copies of "myTestClass", when
    >CLASSNAME is declared as below:
    >#define CLASSNAME "myTestClass"


    Where did you get that understanding from? There *may* be 100 copies,
    but there may be only one. It's up to the compiler to decide.

    >But, there will be only 1 copy of "myTestClass", when
    >CLASSNAME is declared as below:
    >const char* CLASSNAME = "myTestClass" ;


    In principle. There is nothing preventing the compiler from generating
    as many copies as it likes to.

    >Is my understanding correct ? Isn't that an advantage of 'const'
    >over '#define' declaration for constants?


    Not in C, where you can write, at file scope:

    #define CLASSNAME "myTestClass"
    const char *classname = CLASSNAME;

    but you cannot write:

    const char *CLASSNAME = "myTestClass";
    const char *classname = CLASSNAME;

    const cannot replace #define in C: it's not part of the common subset of
    C and C++, despite being a feature of both languages.

    Then again, you would have known this, if you bothered to read the c.l.c
    FAQ before (cross)posting.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 11, 2003
    #7
  8. qazmlp

    Dan Pop Guest

    In <> Pieter Droogendijk <> writes:

    >On Sat, 09 Aug 2003 12:21:29 +0100
    >"Simon Saunders" <> wrote:
    >> On Sat, 09 Aug 2003 12:52:16 +0200, Pieter Droogendijk wrote:
    >>
    >> > On 9 Aug 2003 03:21:14 -0700
    >> > (qazmlp) wrote:
    >> >> As I understand, there will be 100 copies of "myTestClass", when
    >> >> CLASSNAME is declared as below:
    >> >> #define CLASSNAME "myTestClass"
    >> >
    >> > No. "myTestClass" will exist once, and all literals will be translated to a
    >> > pointer to that address.
    >> >
    >> > printf ("%d\n", "foo" - "foo");
    >> > will print 0.

    >>
    >> Not necessarily. Some compilers do this by default, others support it as
    >> an optional feature, while others don't support it at all.

    >
    >I see. Thanks. I guess this is OT after all...


    Nope, it ain't. The exact behaviour of your compiler is OT, but not the
    fact that this behaviour is compiler dependent.

    Most commercial compilers don't optimise identical string literals (by
    default, at least), in order to avoid surprising the user. For the same
    reason, they don't make them read only. This was the documented behaviour
    of pre-ANSI C.

    gcc does the opposite: by default, string literals are read only and
    optimised. If you make them writable, with -fwritable-strings, they are
    no longer optimised.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 11, 2003
    #8
    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. qazmlp

    'const' saves space??

    qazmlp, Aug 9, 2003, in forum: C++
    Replies:
    4
    Views:
    341
    Josephine Schafer
    Aug 11, 2003
  2. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,846
    Bryan Bullard
    Jul 11, 2003
  3. qazmlp

    'const' saves space??

    qazmlp, Aug 9, 2003, in forum: C Programming
    Replies:
    6
    Views:
    286
    Dan Pop
    Aug 11, 2003
  4. qazmlp

    'const' saves space??

    qazmlp, Aug 9, 2003, in forum: C Programming
    Replies:
    3
    Views:
    308
    Chris Torek
    Aug 9, 2003
  5. Javier
    Replies:
    2
    Views:
    533
    James Kanze
    Sep 4, 2007
Loading...

Share This Page