Where to place a constant?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lasse Skyum

    Where to place a constant?

    I have my own string class that I'm generally verry fond of compared to
    std::string. For that same reason I'm trying to improve a little on it.

    Since manny objects in my project will contain strings of empty size "" I
    thought it would be a good idea to share en empty string allocation between
    all uninitialized strings.

    However I have no idea where to place this constant? :-S
    I tried this:

    class CBL_String
    {
    public:
    static char * strEmptyString = ""; // it complaines about this...

    };

    If I do don't initialize it in the class-spec but in a cpp file I'm not sure
    that it is actually initialized before used, by some other global CBL_String
    variable... right?

    char *CBL_String::st rEmptyString = "";


    Suggestions anyone?

    --
    Lasse


  • Alf P. Steinbach

    #2
    Re: Where to place a constant?

    On Mon, 16 Feb 2004 12:23:11 +0100, "Lasse Skyum" <no spam> wrote:
    [color=blue]
    >Suggestions anyone?[/color]

    You'd be much better off using std::string.

    Comment

    • Lasse Skyum

      #3
      Re: Where to place a constant?

      [color=blue][color=green]
      > >Suggestions anyone?[/color]
      >
      > You'd be much better off using std::string.[/color]

      Okay... maybe so, but I don't really se why? CBL_String helps me in manny
      different ways and has never been the source of any bugs in my programs.

      Does std::string have a shared empty string or maybe it uses NULL as an
      empty string?
      If std::string also allocated an empty string if not initialized, then I see
      no reason what so ever to use it...

      BTW, I've also written my own CBL_ArrayList as an equivalent to vector...
      ony differenceis the ability to automatically shrink if a flag is set. For
      some odd reason it also outperformes vector in my insert/remove benchmarks,
      probably just a bunch of settings or something.

      I'm sure all this is also in Boost (or whatever C++ extentions there might
      be) but I've learned so much be writing it my self.

      Anyway... I just wanted to have an initialized constant :-) Might also be
      relevant for other things!

      --
      Lasse


      Comment

      • André Pönitz

        #4
        Re: Where to place a constant?

        Lasse Skyum <no spam> wrote:[color=blue]
        >[color=green][color=darkred]
        >> >Suggestions anyone?[/color]
        >>
        >> You'd be much better off using std::string.[/color]
        >
        > Okay... maybe so, but I don't really se why?[/color]

        Simply because std::string is 'the standard'. Using it makes your code
        better re-usable for starters.
        [color=blue]
        > Does std::string have a shared empty string or maybe it uses NULL as an
        > empty string?[/color]

        This would be an implementation detail.
        [color=blue]
        > If std::string also allocated an empty string if not initialized, then I see
        > no reason what so ever to use it...[/color]

        A decent std::string implementation will make sure that 'std::string s;' is
        _really_ cheap. I would not expect any dynamic allocation in this case.
        [color=blue]
        > BTW, I've also written my own CBL_ArrayList as an equivalent to vector...[/color]

        This strikes me as an even worse idea than having a 'new' string class
        (which might have its uses for certain encoding related reasons)
        [color=blue]
        > ony differenceis the ability to automatically shrink if a flag is set.
        > For some odd reason it also outperformes vector in my insert/remove
        > benchmarks, probably just a bunch of settings or something.[/color]

        Would be nice to see some minimal code demonstrating these results.
        It's not easy to believe unless you dropped some 'important' std::vector
        characteristics .
        [color=blue]
        > I'm sure all this is also in Boost (or whatever C++ extentions there might
        > be) but I've learned so much be writing it my self.[/color]

        Learning is always a good reason to re-write certain standard classes.
        It is rarely a good enough reason for _using_ them, though ;-)

        Andre' [who isn't particularly fond of std::string either but uses it
        nevertheless]

        Comment

        • Rolf Magnus

          #5
          Re: Where to place a constant?

          "Lasse Skyum" <no spam> wrote:
          [color=blue][color=green][color=darkred]
          >> >Suggestions anyone?[/color]
          >>
          >> You'd be much better off using std::string.[/color]
          >
          > Okay... maybe so, but I don't really se why? CBL_String helps me in
          > manny different ways and has never been the source of any bugs in my
          > programs.
          >
          > Does std::string have a shared empty string or maybe it uses NULL as
          > an empty string?[/color]

          I'd say that's implementation specific. Anyway, why do you think you
          need such a shared ... ehm, nothing? I mean, an empty string has no
          data to share. Its size is 0, so there is nothing to read from it, so
          why would you need to share that 'nothing' between empty strings? Btw:
          Do you actually have that many empty strings in real programs?
          [color=blue]
          > If std::string also allocated an empty string if not initialized, then
          > I see no reason what so ever to use it...
          >
          > BTW, I've also written my own CBL_ArrayList as an equivalent to
          > vector... ony differenceis the ability to automatically shrink if a
          > flag is set.[/color]

          If you're happy with your own containers and strings, it's IMHO ok to
          use them. Of course, you'll limit code reusability somewhat.
          [color=blue]
          > For some odd reason it also outperformes vector in my
          > insert/remove benchmarks, probably just a bunch of settings or
          > something.[/color]

          That again depends on the implementation you use, not only on settings.
          [color=blue]
          > I'm sure all this is also in Boost (or whatever C++ extentions there
          > might be) but I've learned so much be writing it my self.[/color]

          That's of course a very good reason to write your own ones, but it is
          not a reason to use them in real code :-)
          [color=blue]
          > Anyway... I just wanted to have an initialized constant :-) Might
          > also be relevant for other things![/color]

          If you want to be sure that your constant is initialized before other
          static objects, you might want to use the singleton pattern.

          Comment

          • Lasse Skyum

            #6
            Re: Where to place a constant?

            [color=blue]
            > Would be nice to see some minimal code demonstrating these results.
            > It's not easy to believe unless you dropped some 'important' std::vector
            > characteristics .[/color]

            Nope, it does all the copy-constructing and inplace new's needed to make it
            work right. However not using allocators, just plain ol' malloc/free.

            Nevermind... that's not what wanted to talk about in the first place.

            --
            Lasse


            Comment

            • Lasse Skyum

              #7
              Re: Where to place a constant?

              [color=blue]
              > If you want to be sure that your constant is initialized before other
              > static objects, you might want to use the singleton pattern.[/color]

              Singleton... well seems to be a lot of work and fiddeling just for
              initializing a constant :-(

              --
              Lasse


              Comment

              • Evan Carew

                #8
                Re: Where to place a constant?

                -----BEGIN PGP SIGNED MESSAGE-----
                Hash: SHA1

                Lasse Skyum wrote:[color=blue][color=green]
                >>Would be nice to see some minimal code demonstrating these results.
                >>It's not easy to believe unless you dropped some 'important' std::vector
                >>characteristi cs.[/color]
                >
                >
                > Nope, it does all the copy-constructing and inplace new's needed to make it
                > work right. However not using allocators, just plain ol' malloc/free.
                >
                > Nevermind... that's not what wanted to talk about in the first place.
                >
                > --
                > Lasse
                >
                >[/color]
                Sounds like that's the source of your performance gain right there. most
                implementations use a new with a goodly bit of code in it to ensure safe
                memory allocation, and if not, then gracefull notification.

                Evan
                -----BEGIN PGP SIGNATURE-----
                Version: GnuPG v1.0.6 (GNU/Linux)
                Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

                iD8DBQFAMMtaoo/Prlj9GScRAov6AJ 948pesdtbnthcOE jUpXBJIKGFVFACf RCe5
                OLn3htYOKLCUgi8 7kIKknHs=
                =ZUUQ
                -----END PGP SIGNATURE-----

                Comment

                • Thomas Matthews

                  #9
                  Re: Where to place a constant?

                  Lasse Skyum wrote:[color=blue]
                  > I have my own string class that I'm generally verry fond of compared to
                  > std::string. For that same reason I'm trying to improve a little on it.
                  >
                  > Since manny objects in my project will contain strings of empty size "" I
                  > thought it would be a good idea to share en empty string allocation between
                  > all uninitialized strings.
                  >
                  > However I have no idea where to place this constant? :-S
                  > I tried this:
                  >
                  > class CBL_String
                  > {
                  > public:
                  > static char * strEmptyString = ""; // it complaines about this...
                  >
                  > };
                  >
                  > If I do don't initialize it in the class-spec but in a cpp file I'm not sure
                  > that it is actually initialized before used, by some other global CBL_String
                  > variable... right?
                  >
                  > char *CBL_String::st rEmptyString = "";
                  >
                  >
                  > Suggestions anyone?
                  >
                  > --
                  > Lasse[/color]

                  In the C++ specification, there is no order to initializing static
                  variables declared in global or local translation unit scope. Thus
                  most people initialize these variables in the constructor. Search
                  the web for "Scott Meyers variable initialization" .

                  If the order of static variable initialization is important, then
                  use the Singleton design pattern or move the variables inside a
                  function. The rule for static variable initialization in a
                  function is "first use".

                  --
                  Thomas Matthews

                  C++ newsgroup welcome message:

                  C++ Faq: http://www.parashift.com/c++-faq-lite
                  C Faq: http://www.eskimo.com/~scs/c-faq/top.html
                  alt.comp.lang.l earn.c-c++ faq:

                  Other sites:
                  http://www.josuttis.com -- C++ STL Library book
                  http://www.sgi.com/tech/stl -- Standard Template Library

                  Comment

                  • kevin collins

                    #10
                    Re: Where to place a constant?

                    Lasse Skyum <no spam> wrote in message
                    news:4030a89a$0 $29313$edfadb0f @dread15.news.t ele.dk...[color=blue]
                    > I have my own string class that I'm generally verry fond of compared to
                    > std::string. For that same reason I'm trying to improve a little on it.
                    >
                    > Since manny objects in my project will contain strings of empty size "" I
                    > thought it would be a good idea to share en empty string allocation[/color]
                    between[color=blue]
                    > all uninitialized strings.
                    >
                    > However I have no idea where to place this constant? :-S
                    > I tried this:
                    >
                    > class CBL_String
                    > {
                    > public:
                    > static char * strEmptyString = ""; // it complaines about this...
                    >
                    > };
                    >
                    > If I do don't initialize it in the class-spec but in a cpp file I'm not[/color]
                    sure[color=blue]
                    > that it is actually initialized before used, by some other global[/color]
                    CBL_String[color=blue]
                    > variable... right?
                    >
                    > char *CBL_String::st rEmptyString = "";
                    >
                    >
                    > Suggestions anyone?
                    >
                    > --
                    > Lasse[/color]

                    This compiles, try this.

                    class C
                    {
                    public:
                    static char* sk;
                    };
                    char* C::sk = "";

                    Put all that in a header file.

                    This thread is a good example of why you should not tell people what you are
                    doing. You will find that a great many of the regulars are much smarter
                    than you and instead of answering your question they will lecture you on how
                    and why you are doing the wrong damn thing.




                    Comment

                    • Rolf Magnus

                      #11
                      Re: Where to place a constant?

                      "Lasse Skyum" <no spam> wrote:
                      [color=blue]
                      >[color=green]
                      >> If you want to be sure that your constant is initialized before other
                      >> static objects, you might want to use the singleton pattern.[/color]
                      >
                      > Singleton... well seems to be a lot of work and fiddeling just for
                      > initializing a constant :-([/color]

                      It can be as simple as:

                      class CBL_String
                      {
                          static const char * strEmptyString( )
                      {
                      const char* const empty = "";
                      return empty;
                      }
                      };

                      Actually, even this should work:

                      class CBL_String
                      {
                          static const char * strEmptyString( )
                      {
                      return "";
                      }
                      };

                      Comment

                      • Nick Hounsome

                        #12
                        Re: Where to place a constant?


                        "Lasse Skyum" <no spam> wrote in message
                        news:4030a89a$0 $29313$edfadb0f @dread15.news.t ele.dk...[color=blue]
                        > I have my own string class that I'm generally verry fond of compared to
                        > std::string. For that same reason I'm trying to improve a little on it.
                        >
                        > Since manny objects in my project will contain strings of empty size "" I
                        > thought it would be a good idea to share en empty string allocation[/color]
                        between[color=blue]
                        > all uninitialized strings.
                        >
                        > However I have no idea where to place this constant? :-S
                        > I tried this:
                        >
                        > class CBL_String
                        > {
                        > public:
                        > static char * strEmptyString = ""; // it complaines about this...[/color]

                        This is wrong for several reasons besides initialization:
                        strEmptyString = new char[10]; // this will compile!!
                        strEmptyString[3] = '!'; // This will also compile!!!

                        What you want is a constant string NOT an non-constant pointer to a
                        non-constant string.
                        This is declared as:

                        static const char EMPTY_STRING[]; // NB CONST
                        [color=blue]
                        >
                        > };
                        >[/color]

                        Define in cpp file:

                        const char CBL_String::EMP TY_STRING[] = "";

                        This does not require runtime initialization.
                        IF you don't use it in inline methods then it is better still to make it
                        file static rather than class static.
                        [color=blue]
                        > If I do don't initialize it in the class-spec but in a cpp file I'm not[/color]
                        sure[color=blue]
                        > that it is actually initialized before used, by some other global[/color]
                        CBL_String[color=blue]
                        > variable... right?
                        >
                        > char *CBL_String::st rEmptyString = "";[/color]

                        None of this simple stuff requires runtime initialization it just goes in
                        the data section
                        of the object file.
                        [color=blue]
                        >
                        >
                        > Suggestions anyone?
                        >[/color]

                        Don't use your own string - it may be wonderful but there are millions of
                        people out there who know
                        the std::string and only one who knows CBL_String.

                        What is the cost to your employer of you designing, coding, testing and
                        documenting your string?
                        Just documenting it as well as the std string would cost $1000!
                        What is the cost of future maintainers having to read your string docs
                        instead of just using stuff they already know?


                        Comment

                        • Lasse Skyum

                          #13
                          Re: Where to place a constant?

                          [color=blue]
                          > It can be as simple as:
                          >
                          > class CBL_String
                          > {
                          > static const char * strEmptyString( )
                          > {
                          > const char* const empty = "";
                          > return empty;
                          > }
                          > };[/color]

                          Thanks Rolf, I'll check that one out!

                          --
                          Lasse


                          Comment

                          • Lasse Skyum

                            #14
                            Re: Where to place a constant?


                            [color=blue]
                            > This compiles, try this.
                            >
                            > class C
                            > {
                            > public:
                            > static char* sk;
                            > };
                            > char* C::sk = "";
                            >
                            > Put all that in a header file.[/color]

                            Thanks, I'll try it out... however not really understanding when exactly the
                            initialization is taking place.
                            [color=blue]
                            > This thread is a good example of why you should not tell people what you[/color]
                            are[color=blue]
                            > doing. You will find that a great many of the regulars are much smarter
                            > than you and instead of answering your question they will lecture you on[/color]
                            how[color=blue]
                            > and why you are doing the wrong damn thing.[/color]

                            Yes, that tend to happen every time I post here :-(

                            --
                            Lasse



                            Comment

                            • Alf P. Steinbach

                              #15
                              Re: Where to place a constant?

                              On Tue, 17 Feb 2004 10:23:01 +0100, "Lasse Skyum" <no spam> wrote:[color=blue]
                              >
                              >On Mon, 16 Feb 2004 17:13:51 GMT, "kevin collins" <kevcollins@hot mail.com> wrote:[color=green]
                              >> This compiles, try this.
                              >>
                              >> class C
                              >> {
                              >> public:
                              >> static char* sk;
                              >> };
                              >> char* C::sk = "";
                              >>
                              >> Put all that in a header file.[/color]
                              >
                              >Thanks, I'll try it out...[/color]

                              Be sure to try it out with the header-file used in two different
                              compilation units of the same program.

                              In that case you'll get a linker error.

                              You can¹ avoid the linker error by adding 'const' here and there, but the
                              effect of that is that you specify internal linkage, i.e. multiple copies,
                              which is what you were trying to avoid IIRC (not that # of copies matters).


                              [color=blue]
                              > however not really understanding when exactly the
                              >initializati on is taking place.[/color]

                              Initialization of namespace level statics in a compilation unit is
                              performed before the first call of a function in that compilation unit,
                              including call of a constructor.

                              [color=blue][color=green]
                              >> This thread is a good example of why you should not tell people
                              >> what you are doing.[/color][/color]

                              On the contrary, this thread is a good example of why you should tell
                              people what you are doing.

                              The best general advice so far is to use std::string.

                              The best advice so far regarding how to provide a string constant is
                              the C++ singleton pattern (a function containing a static variable).

                              [color=blue][color=green]
                              >> You will find that a great many of the regulars are much smarter
                              >> than you and instead of answering your question they will lecture you on
                              >> how and why you are doing the wrong damn thing.[/color]
                              >
                              >Yes, that tend to happen every time I post here :-([/color]



                              ¹) At least I think so. But I have fever and am not thinking clearly,
                              and darned if I'm going to check out something you should check out.

                              Comment

                              Working...