This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Core dump constructing a C++ string with NULL


Tom Browder <tom.browder@gmail.com> writes:

> It would be nice if this would construct an empty string:
>
>   #include <string>
>   std::string s(0);
>
> At the moment that gives a core dump.
>
> Is that defined behavior according to the standard?  If not, what
> about a g++ enhancement?

The standard says that you may only pass a valid pointer to the
std::string constructor.  You are passing an invalid pointer, so your
code is undefined.

As to whether libstdc++ should check for NULL, that is a perennial
question: should we slow down the library a tiny bit for everybody in
order to do better handling of an undefined case for those who find it
more convenient?  There is no clearly correct answer.

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]