Topic: add a null to a std::string.
Author: "Daniel T." <postmaster@earthlink.net>
Date: Fri, 24 Feb 2006 12:57:29 CST Raw View
In article <468s1qF9ru5hU1@individual.net>,
Dietmar Kuehl <dietmar_kuehl@yahoo.com> wrote:
> JustSomeGuy wrote:
> > This is what I've written but I'm not sure if I've garanteed to have
> > the c_str() method return a buffer that is null padded.
>
> The method 'c_str()' will return a pointer to a null-terminated
> representation of the 'std::string's content. That is, you will
> have an extra null-character tagged on to the 'std::string's
> content. If the 'std::string's content contains null-characters,
> you cannot process the result of 'c_str()' with the 'str...()'
> functions because these might stop at the first embedded
> null-character. Thus, you probably should use the 'data()' method
> instead because this has the same effect as 'c_str()' except that
> it does not add an extra null-character.
Let's say I have a string class that doesn't hold its data in a
contiguous array but still wants to conform to the standard. This would
mean I have to create an array and fill it whenever c_str is called (and
I'm free to delete it whenever a non-const member-function is called.)
Would my class be standard conforming if I only made a c_str array up to
the first null? (I expect that data would have to have all the
characters.)
In other words given:
int main()
{
std::string s = "Hello! World";
s[6] = 0;
const char* foo = s.c_str();
char bar[6];
std::strcpy( bar, foo + 7 );
std::cout << bar;
}
Does the standard define the output of the above program?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Kai-Uwe Bux <jkherciueh@gmx.net>
Date: Fri, 24 Feb 2006 14:19:26 CST Raw View
Daniel T. wrote:
> In article <468s1qF9ru5hU1@individual.net>,
> Dietmar Kuehl <dietmar_kuehl@yahoo.com> wrote:
>
>> JustSomeGuy wrote:
>> > This is what I've written but I'm not sure if I've garanteed to have
>> > the c_str() method return a buffer that is null padded.
>>
>> The method 'c_str()' will return a pointer to a null-terminated
>> representation of the 'std::string's content. That is, you will
>> have an extra null-character tagged on to the 'std::string's
>> content. If the 'std::string's content contains null-characters,
>> you cannot process the result of 'c_str()' with the 'str...()'
>> functions because these might stop at the first embedded
>> null-character. Thus, you probably should use the 'data()' method
>> instead because this has the same effect as 'c_str()' except that
>> it does not add an extra null-character.
>
> Let's say I have a string class that doesn't hold its data in a
> contiguous array but still wants to conform to the standard. This would
> mean I have to create an array and fill it whenever c_str is called (and
> I'm free to delete it whenever a non-const member-function is called.)
>
> Would my class be standard conforming if I only made a c_str array up to
> the first null? (I expect that data would have to have all the
> characters.)
>
> In other words given:
>
> int main()
> {
> std::string s = "Hello! World";
> s[6] = 0;
> const char* foo = s.c_str();
> char bar[6];
> std::strcpy( bar, foo + 7 );
> std::cout << bar;
> }
>
> Does the standard define the output of the above program?
>
>
The standard specifies:
21.3.6/1 const charT* c_str() const;
Returns: A pointer to the initial element of an array of length size() + 1
whose first size() elements equal the corresponding elements of the string
controlled by *this and whose last element is a null character specified by
charT().
Thus, you are not allowed to stop at the first 0 char. The size and contents
of the array pointed to by the return value of c_str() are completely
specified.
Best
Kai-Uwe Bux
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]