Topic: how should string class initialize itself
Author: chang@mjs1.moderated.news.pipex.net (Jianlin Chang)
Date: 1998/08/08 Raw View
string s1;
const char* p = s1.c_str();
after these, p seems to be a garbage pointer, and
cout << p[0]
output garbage and is reported as error by memory detection tool such
as purify.
Someone argue that either p has to be 0 or p[0] has to be '\0' for the
string calss, and the string class shouldn't give garbage for c_str()
for a string object initialized using 'string s1'. Is this correct?
Thanks.
--
When reply, please add .phy.queensu.ca to email address, sorry, this
is to prevent email spams.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alwyn Thomas <alwyn@dircon.co.uk>
Date: 1998/08/09 Raw View
Jianlin Chang wrote:
> string s1;
> const char* p = s1.c_str();
>
> after these, p seems to be a garbage pointer, and
> cout << p[0]
> output garbage and is reported as error by memory detection tool such
> as purify.
>
> Someone argue that either p has to be 0
I do not think that std::string::c_str() is allowed to return a null pointer
under any circumstances.
> or p[0] has to be '\0' for the
> string calss, and the string class shouldn't give garbage for c_str()
> for a string object initialized using 'string s1'. Is this correct?
My understanding is that a string initialised by the default constructor
should have size() return zero. Under such circumstances, c_str() should
return a pointer to a null character in copyable memory.
So I would say that your implementation does not conform to the C++ standard
as I know it.
Alwyn
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Edward Diener <eddielee@abraxis.com>
Date: 1998/08/10 Raw View
string s1;
should create an empty string.
const char* p = s1.c_str();
p should be a valid pointer to an array of characters with a single null (0)
value.
Jianlin Chang wrote:
> string s1;
> const char* p = s1.c_str();
>
> after these, p seems to be a garbage pointer, and
> cout << p[0]
> output garbage and is reported as error by memory detection tool such
> as purify.
>
> Someone argue that either p has to be 0 or p[0] has to be '\0' for the
> string calss, and the string class shouldn't give garbage for c_str()
> for a string object initialized using 'string s1'. Is this correct?
{ quoted sig and both banners removed, please do not quote these. -jep }
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]