Connecting Tech Pros Worldwide Help | Site Map

stl string initialization question

  #1  
Old July 19th, 2005, 07:55 PM
Thomas
Guest
 
Posts: n/a
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







  #2  
Old July 19th, 2005, 07:55 PM
David B. Held
Guest
 
Posts: n/a

re: stl string initialization question


"Thomas" <tdineen@att.com> wrote in message
news:bls56t$4dd18@kcweb01.netnews.att.com...[color=blue]
> [...]
> Platform info: Solaris 2.8 - Forte 6.2 compiler[/color]

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


  #3  
Old July 19th, 2005, 07:55 PM
Julián Albo
Guest
 
Posts: n/a

re: stl string initialization question


Thomas escribió:
[color=blue]
> It appears that the string class does not check for null pointers on[/color]

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.
  #4  
Old July 19th, 2005, 07:55 PM
Rob Williscroft
Guest
 
Posts: n/a

re: stl string initialization question


Thomas wrote in news:bls56t$4dd18@kcweb01.netnews.att.com:

[snip]
[color=blue]
>
> It appears that the string class does not check for null pointers on
> initialization. Am getting core dump. Stack trace indicates unhandled
> exception.
>[/color]

AFAICT this is Standard conforming behaviour.
[color=blue]
> 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?
>[/color]

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/
  #5  
Old July 19th, 2005, 07:56 PM
Jonathan Mcdougall
Guest
 
Posts: n/a

re: stl string initialization question


> > Platform info: Solaris 2.8 - Forte 6.2 compiler[color=blue]
>
> Specific implementations are off topic in this newsgroup:[/color]

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


Jonathan


  #6  
Old July 19th, 2005, 07:56 PM
WW
Guest
 
Posts: n/a

re: stl string initialization question


Thomas wrote:[color=blue]
> 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.[/color]

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


  #7  
Old July 19th, 2005, 07:56 PM
Jonathan Mcdougall
Guest
 
Posts: n/a

re: stl string initialization question


>The following used to work using the Lucent SCL but now[color=blue]
> fails with STL.
>
>
> string env = getenv("ENV_VALUE");
> // string env(getenv("ENV_VALUE")); // tried this just in case it
> was problem with operator=.[/color]

The operator=() is not used here, the copy constructor is.
[color=blue]
> if(env.empty()){
> // handle this situation
> }
>
>
> It appears that the string class does not check for null pointers on
> initialization.[/color]

This is the expected behavior.
[color=blue]
>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?[/color]

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

if ( env == 0 )
return "";

return std::string(name);
}


Jonathan


  #8  
Old July 19th, 2005, 07:56 PM
David B. Held
Guest
 
Posts: n/a

re: stl string initialization question


"Jonathan Mcdougall" <jonathanmcdougall@DELyahoo.ca> wrote in message
news:RKigb.49639$282.681221@weber.videotron.net...[color=blue][color=green][color=darkred]
> > > Platform info: Solaris 2.8 - Forte 6.2 compiler[/color]
> >
> > Specific implementations are off topic in this newsgroup:[/color]
>
> It ain`t a "specific implementations". Make sure you know
> what you`re talking about.[/color]

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:
[color=blue][color=green]
> > The following used to work using the Lucent SCL but
> > now fails with STL.[/color][/color]

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


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialization of static class variables. Difference between POD and std::string HerbD answers 2 April 5th, 2007 06:27 PM
Object initialization and xml question BCC answers 0 July 23rd, 2005 02:27 AM
I need Help please Slugger819 answers 4 July 23rd, 2005 01:31 AM
Aaaargh! noob question static string array initialization anti-guru answers 2 July 22nd, 2005 07:31 PM