stl string initialization question

Discussion in 'C++' started by Thomas, Oct 6, 2003.

  1. Thomas

    Thomas Guest

    I have a class that uses environment variables as part of its
    initialization. The following used to work using the Lucent SCL but now
    fails with STL.


    string env = getenv("ENV_VALUE");
    // string env(getenv("ENV_VALUE")); // tried this just in case it
    was problem with operator=.
    if(env.empty()){
    // handle this situation
    }


    It appears that the string class does not check for null pointers on
    initialization. Am getting core dump. Stack trace indicates unhandled
    exception.

    I dont' have a choice in using the environment variable at to set this
    variable.

    I can work around this easily enough, but don't think I should have to.
    Anyone seen this before?

    Platform info: Solaris 2.8 - Forte 6.2 compiler

    Thanks,

    Thomas
     
    Thomas, Oct 6, 2003
    #1
    1. Advertising

  2. "Thomas" <> wrote in message
    news:bls56t$...
    > [...]
    > Platform info: Solaris 2.8 - Forte 6.2 compiler


    Specific implementations are off topic in this newsgroup:

    http://www.slack.net/~shiva/welcome.txt

    I suggest you try c.l.c++.m, which is much friendlier and
    much less anal about moderation. And yes, compiler bugs
    are sometimes discussed there.

    Dave



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
     
    David B. Held, Oct 6, 2003
    #2
    1. Advertising

  3. Thomas escribiĆ³:

    > It appears that the string class does not check for null pointers on


    Yes. In many cases it is unnecessary, then when you need it you must do
    it by hand.

    You can write a function like:

    inline const char * allow_null (const char * str)
    {
    if (! str)
    return "";
    return str;
    }

    And then:

    string env= allow_null (getenv ("ENV_VALUE") );

    Regards.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Oct 6, 2003
    #3
  4. Thomas wrote in news:bls56t$:

    [snip]

    >
    > It appears that the string class does not check for null pointers on
    > initialization. Am getting core dump. Stack trace indicates unhandled
    > exception.
    >


    AFAICT this is Standard conforming behaviour.

    > I dont' have a choice in using the environment variable at to set this
    > variable.
    >
    > I can work around this easily enough, but don't think I should have
    > to. Anyone seen this before?
    >


    Yup.

    If you've been relying on this behaviour elseware:

    inline char const *null_to_empty( char const *s )
    {
    return s ? s : 0;
    }

    std::string env = null_to_empty( std::getenv( "ENV_VALUE" ) );

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Oct 6, 2003
    #4
  5. > > Platform info: Solaris 2.8 - Forte 6.2 compiler
    >
    > Specific implementations are off topic in this newsgroup:


    It ain`t a "specific implementations". Make sure you know what
    you`re talking about.


    Jonathan
     
    Jonathan Mcdougall, Oct 6, 2003
    #5
  6. >The following used to work using the Lucent SCL but now
    > fails with STL.
    >
    >
    > string env = getenv("ENV_VALUE");
    > // string env(getenv("ENV_VALUE")); // tried this just in case it
    > was problem with operator=.


    The operator=() is not used here, the copy constructor is.

    > if(env.empty()){
    > // handle this situation
    > }
    >
    >
    > It appears that the string class does not check for null pointers on
    > initialization.


    This is the expected behavior.

    >Am getting core dump. Stack trace indicates unhandled
    > exception.
    >
    > I dont' have a choice in using the environment variable at to set this
    > variable.
    >
    > I can work around this easily enough, but don't think I should have to.
    > Anyone seen this before?


    inline std::string my_getenv(const char* name)
    {
    const char* env = getenv(name);

    if ( env == 0 )
    return "";

    return std::string(name);
    }


    Jonathan
     
    Jonathan Mcdougall, Oct 6, 2003
    #6
  7. Thomas

    WW Guest

    Thomas wrote:
    > I have a class that uses environment variables as part of its
    > initialization. The following used to work using the Lucent SCL but
    > now fails with STL.
    >
    >
    > string env = getenv("ENV_VALUE");
    > // string env(getenv("ENV_VALUE")); // tried this just in
    > case it was problem with operator=.
    > if(env.empty()){
    > // handle this situation
    > }
    >
    >
    > It appears that the string class does not check for null pointers on
    > initialization. Am getting core dump. Stack trace indicates unhandled
    > exception.


    Simon says:

    ===
    21.3.1 basic_string constructors

    basic_string(const charT* s, const Allocator& a = Allocator());

    Paragraph 9:

    Requires: s shall not be a null pointer.
    ===

    So you need to make sure no to pass a null pointer.

    --
    WW aka Attila
     
    WW, Oct 6, 2003
    #7
  8. "Jonathan Mcdougall" <> wrote in message
    news:RKigb.49639$...
    > > > Platform info: Solaris 2.8 - Forte 6.2 compiler

    > >
    > > Specific implementations are off topic in this newsgroup:

    >
    > It ain`t a "specific implementations". Make sure you know
    > what you`re talking about.


    He didn't ask if checking for null pointers was conforming
    behaviour. He asked why it doesn't work on his implementation,
    and then he specified what that was. You should read the
    newsgroup guidelines:

    http://www.slack.net/~shiva/welcome.txt

    http://www.slack.net/~shiva/welcome.txt

    I've included it twice in case you have problems with the
    first link. Here is another quote from the OP:

    > > The following used to work using the Lucent SCL but
    > > now fails with STL.


    Where is "Lucent SCL" specified in the standard? This is
    a C++ newsgroup, not a Lucent newsgroup!

    Dave



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
     
    David B. Held, Oct 6, 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. JKop
    Replies:
    10
    Views:
    948
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    445
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    462
    Ron Natalie
    Dec 11, 2005
  4. toton
    Replies:
    5
    Views:
    935
    Victor Bazarov
    Sep 28, 2006
  5. Mr. K.V.B.L.
    Replies:
    6
    Views:
    1,080
    Richard Herring
    Sep 16, 2008
Loading...

Share This Page