Core dump constructing a C++ string with NULL
Jonathan Wakely
jwakely.gcc@gmail.com
Fri Jan 14 18:04:00 GMT 2011
On 14 January 2011 17:43, Tom Browder wrote:
> On Fri, Jan 14, 2011 at 11:38, Ian Lance Taylor <iant@google.com> wrote:
>> Tom Browder <tom.browder@gmail.com> writes:
>>
>>> 2. Isn't a '\0' an empty string in the string context?
>>
>> I thought you were asking about std::string(0), which is quite a
>> different matter from std::string('\0').
>
> What I'm poorly trying to say is that, even if the string is passed a
> null pointer, that appears to be an empty string in the context of a
> string, so an option to accept it as such is not unreasonable.
I don't know what you mean by the context of a string, but I don't
think it's the case that a null pointer is the same as an empty
string, in any context. The C and C++ standard libraries certainly
doesn't treat them the same.
If you mean that in the string's constructor, 0 and "" are the same,
that's definitely not true.
One is a null pointer and one is a pointer to an array of one char.
> I understand that the null pointer is probably an error up stream, and
> this might not be portable. But the whole point of C++ striings I
> thought was to be a safe string immune from pointer problems.
I don't think that's "the whole point" at all - automatic memory
management is at least as important IMHO. They're certainly not
immune to problems caused by unterminated strings, or null pointers.
More information about the Gcc-help
mailing list