C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

declaration then append OK?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
sorcer
Guest





PostPosted: Tue Dec 20, 2005 11:13 am    Post subject: declaration then append OK? Reply with quote



Can I do the following:

....
std::string NewString;
....
NewString += (const char *)SomeChars;


Is it OK to append to the string as the first "assignment"? Windows and
Linux are OK (well, so far), but Solaris is breaking, and I'm not sure
if this is the cause, due to stack corruption.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
benben
Guest





PostPosted: Tue Dec 20, 2005 1:38 pm    Post subject: Re: declaration then append OK? Reply with quote



sorcer wrote:
Quote:
Can I do the following:

...
std::string NewString;
...
NewString += (const char *)SomeChars;

If SomeChars isn't a const char* (for you have casted it to such type)
perhaps you can tell us what it is.

Quote:


Is it OK to append to the string as the first "assignment"? Windows and
Linux are OK (well, so far), but Solaris is breaking, and I'm not sure
if this is the cause, due to stack corruption.

By "to append to the string as the first assignment" do you mean:

a. std::string NewString = "blah";or
b. std::string NewString; NewString = "blah"; or
c. std::string NewString; NewString += "blah"; ?

I doubt your problem has anything to do with the operating system.
Perhaps it is the library that broke.

Regards,
Ben

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
hroussel_at_delphes.com@h
Guest





PostPosted: Tue Dec 20, 2005 2:34 pm    Post subject: Re: declaration then append OK? Reply with quote



Hi!
You ask:

Quote:
Can I do the following:

...
std::string NewString;
...
NewString += (const char *)SomeChars;

Without a sample that compile, the only answer is:
Maybe.

The fact that it crash on solaris is a good indication that your code
has a problem.

Wonixen


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Tue Dec 20, 2005 2:34 pm    Post subject: Re: declaration then append OK? Reply with quote

sorcer wrote:
Quote:
std::string NewString;
...
NewString += (const char *)SomeChars;


Is it OK to append to the string as the first "assignment"?

Yes.

Quote:
Windows and Linux are OK (well, so far), but Solaris is breaking,

All three are operating systems and there are compilers out there that run
on all three of them. In order to really specify the system you're on, you
should mention the compiler/version plus the OS/version, but I doubt that
that is the problem here.

Quote:
and I'm not sure if this is the cause, due to stack corruption.

If the code compiles with the cast removed, it is probably ok, but with the
cast it is impossible to tell. It might also make a difference what
'SomeChars' really is. Otherwise, a small testprogram that demonstrates the
faulty behaviour would be necessary.

As a general rule, never use casts unless you have to. If you get errors or
warnings which you don't understand, casting is wrong, even if the errors
or warnings disappear.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Lars Tetzlaff
Guest





PostPosted: Tue Dec 20, 2005 7:02 pm    Post subject: Re: declaration then append OK? Reply with quote

sorcer schrieb:
Quote:
Can I do the following:

...
std::string NewString;
...
NewString += (const char *)SomeChars;


Why do you need the '(const char *)'?

What is 'SomeChars'?

Lars

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Paul Floyd
Guest





PostPosted: Wed Dec 21, 2005 3:27 am    Post subject: Re: declaration then append OK? Reply with quote

On 20 Dec 2005 06:13:12 -0500, sorcer <sklekas (AT) websense (DOT) com> wrote:
Quote:
Can I do the following:

...
std::string NewString;
...
NewString += (const char *)SomeChars;


Is it OK to append to the string as the first "assignment"? Windows and

I've seen some problems with Sun Studio 11 and initialization from a
temporary, like

std::string newString = aString + anotherString;

when using optimize options.

My guess is that a patch is in the pipeline.

A bientot
Paul
--
Paul Floyd http://paulf.free.fr (for what it's worth)
Surgery: ennobled Gerald.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Paul Floyd
Guest





PostPosted: Thu Dec 22, 2005 3:26 am    Post subject: Re: declaration then append OK? Reply with quote

On 20 Dec 2005 22:27:06 -0500, Paul Floyd <root (AT) 127 (DOT) 0.0.1> wrote:

[snip - problem with initialization from a temporary on Solaris]

Quote:
My guess is that a patch is in the pipeline.

... no sooner said than done, it seems. There are now a couple of
patches for the common compiler backend and C++ compiler that seem to
fix the problem.

A bientot
Paul
--
Paul Floyd http://paulf.free.fr (for what it's worth)
Surgery: ennobled Gerald.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.