Article View: gnu.g++.bug
Article #1967Un-initialized return Strings
From: Tim Day
Date: Wed, 10 May 1989 10:58
Date: Wed, 10 May 1989 10:58
50 lines
1736 bytes
1736 bytes
I am rather confused by the following...
Most of the time, a String() seems to be the equivalent of a String(""),
(as I'd expect), but in the below code attempting to output an unitialized
String returned from a function as a String causes a segmentation fault,
which weird since normally uninitialised Strings, and functions returning
Strings, work fine. It also seems odd that returning a StrTmp should fix
the problem. (As will always declaring
String bleah;
as
String bleah("");
)
------------------------------
#include <stream.h>
#include <String.h>
String hot() // Returning un-init. String as String bombs
{ String bleah;
return bleah;
}
StrTmp digital() // ..but returning it as StrTmp is OK
{ String bleah;
return bleah;
}
String dog() // ..as is returning it as String, once loaded
{ String bleah;
bleah=String("WOOF");
return bleah;
}
main()
{ if (String() == String(""))
cerr << "Uninitialised string == empty string\n";
if (String() != String(""))
cerr << "Uninitialised string != empty string\n";
cerr << "'" << String("") << "'\n"; // OK
cerr << "'" << String() << "'\n"; // OK
cerr << "'" << dog() << "'\n"; // OK
cerr << "'" << digital() << "'\n"; // OK
cerr << "'" << hot() << "'\n"; // Ouch!
exit(0);
}
-------------------------------
Tim
Technical Details
Message-ID:
<8905102120.AA29080@AENEAS.MIT.EDU>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!CS.UCL.AC.UK!T.DayShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!CS.UCL.AC.UK!T.Day From: T.Day@CS.UCL.AC.UK (Tim Day) Newsgroups: gnu.g++.bug Subject: Un-initialized return Strings Message-ID: <8905102120.AA29080@AENEAS.MIT.EDU> Date: 10 May 89 10:58:56 GMT Sender: daemon@tut.cis.ohio-state.edu Distribution: gnu Organization: GNUs Not Usenet Lines: 50