stl string initialization question

T

Thomas

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
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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.
 
R

Rob Williscroft

Thomas wrote in
[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.
 
J

Jonathan Mcdougall

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
 
J

Jonathan Mcdougall

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
 
W

WW

Thomas said:
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.
 
D

David B. Held

Jonathan Mcdougall said:
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:

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

Dave
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top