Topic: stringlibrary: data() versus c_str() and nullbyte
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1996/05/02 Raw View
In article <jc.830939057@atcmpg> jc@atcmp.nl (Jan Christiaan van Winkel)
writes:
|> While using the libg++ 2.7.1 implementation of the string library,
|> I encountered a problem.
|> Suppose the length of a string has decreased:
|> string s("hello");
|> s="hi";
|> what should s.data() return compared to s.c_ptr() ?
s.data should return a pointer to two bytes, containing 'h', 'i'.
s.c_ptr should return a poitner to three bytes, containing 'h', 'i',
'\0'.
|> Example:
|> #include <string>
|> #include <iostream.h>
|> main() {
|> string s("hello");
|> s="hi";
|> cout << "Length: " << s.length() << endl;
|> cout << s << endl;
|> cout << s.data() << endl;
|> cout << s.c_str() << endl;
|> }
|> g++ 2.7.2 with libg++-2.7.1 gives:
|> Length: 2
|> hi
|> hillo
|> hi
|> Is this correct?
Yes and no. I don't think you can fault g++'s behavior here, but the
line `cout << s.data() << endl' is undefined behavior. Outputting
"hillo" seems as good an undefined behavior as any. (Ideally, this line
would generate a core dump. But g++ is certainly not alone in not doing
so:-). Does anyone know of an implementation which will systematically
generate a core dump in such cases?)
(For those who miss the point: `operator<<( ostream& , char const* )'
requires a null terminated string as second argument. `string::data()'
does not return a pointer to a null terminated string, but an array of
string::length() char's.)
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: jbuck@Synopsys.COM (Joe Buck)
Date: 1996/05/02 Raw View
jc@atcmp.nl (Jan Christiaan van Winkel) writes:
>While using the libg++ 2.7.1 implementation of the string library,
>I encountered a problem.
It appears that libg++ is matching a later version of the draft
standard than April 1995.
>Suppose the length of a string has decreased:
>
>string s("hello");
>s="hi";
>
>what should s.data() return compared to s.c_ptr() ?
s.data() should point to an 'h' that is followed by an 'i' that can be
followed by anything ("hillo" is one possibility). s.c_ptr() must point
to the null-terminated C string "hi". In the libg++ implementation
c_ptr() adds the null terminator. I've seen others (e.g. ObjectSpace)
where c_ptr copies the whole string somewhere else.
>The 4/95 WP sais data() returns c_str() if size() is nonzero,
>otherwise a null pointer.
That language is gone from the January 96 draft; the description of
data() is completely different:
const charT* data() const;
Returns:
If size() is nonzero, the member returns a pointer to the initial
element of an array whose first size() elements equal the corre-
sponding elements of the string controlled by *this. If size() is
zero, the member returns a non-null pointer that is copyable and can
have zero added to it.
Requires:
The program shall not alter any of the values stored in the charac-
ter array. Nor shall the program treat the returned value as a
valid pointer value after any subsequent call to a non- const member
function of basic_string that designates the same object as this.
I am puzzled by the clause "that is copyable and can have zero added to it".
I suspect that the author was thinking of an implementation where the
effect of c_str is to add a null character at data()+size(), but I don't
know what is meant by putting this strange phrase into the standard.
--
-- Joe Buck <jbuck@synopsys.com> (not speaking for Synopsys, Inc)
Work for something because it is good,
not just because it stands a chance to succeed. -- Vaclav Havel
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jc@atcmp.nl (Jan Christiaan van Winkel)
Date: 1996/05/01 Raw View
Hello,
While using the libg++ 2.7.1 implementation of the string library,
I encountered a problem.
Suppose the length of a string has decreased:
string s("hello");
s="hi";
what should s.data() return compared to s.c_ptr() ?
Example:
#include <string>
#include <iostream.h>
main() {
string s("hello");
s="hi";
cout << "Length: " << s.length() << endl;
cout << s << endl;
cout << s.data() << endl;
cout << s.c_str() << endl;
}
g++ 2.7.2 with libg++-2.7.1 gives:
Length: 2
hi
hillo
hi
Is this correct?
The 4/95 WP sais data() returns c_str() if size() is nonzero,
otherwise a null pointer.
This does not explain the output above...
JC
--
___ __ ____________________________________________________________________
|/ \ Jan Christiaan van Winkel Tel: +31 24 3527252 jc@atcmp.nl
| AT Computing P.O. Box 1428 6501 BK Nijmegen The Netherlands
__/ \__/ __________ http://www.nl.net/~atcmp/staf/jc.gif ____________________
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]