473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

stl string initialization question

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



Jul 19 '05 #1
7 5341
"Thomas" <td*****@att.com> wrote in message
news:bl**********@kcweb01.netnews.att.com...
[...]
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
Jul 19 '05 #2
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.
Jul 19 '05 #3
Thomas wrote in news:bl**********@kcweb01.netnews.att.com:

[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/
Jul 19 '05 #4
> > 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
Jul 19 '05 #5
WW
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
Jul 19 '05 #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
Jul 19 '05 #7
"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:RK********************@weber.videotron.net...
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
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
4
by: Russell Silva | last post by:
I have a class A with a member variable type vector<foo> which is initialized upon construction (no standard () constructor): class A { protected: vector<foo> foo_vector; public: A(const...
8
by: Baloff | last post by:
Hello I am not sure why my compiler will not initialize string e1("sam"); and will initialize string e1 = "sam"; here is my code and the error. thanks alot
6
by: Rudolf Bargholz | last post by:
Hi , I have the following tables ------------- PAX: Id Order_Id Name Position
5
by: Mark | last post by:
Hello - I'm using VB .Net 2003 connected to Access 2002 via the OLE DB Jet Engine driver. When I open the connection in the Server Explorer, it works fine. But when I build the app I get this...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.