From -8216216352488438484
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,1ac4d0270ad32ac8
X-Google-Attributes: gidf78e5,public
From: Karl Nelson <kenelson@ece.ucdavis.edu>
Subject: Re: C++ Strings and NULL
Date: 1998/09/29
Message-ID: <6upcf1$kl8$2@mark.ucdavis.edu>#1/1
X-Deja-AN: 395901599
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <6u9217$cj9$1@mark.ucdavis.edu> <6uj3fq$dm4$1@rocky.jgk.org> <slrn6vtg4a.sg9.sbnaran@localhost.localdomain>
X-Original-Date: 29 Sep 1998 01:17:21 GMT
Organization: University of California, Davis
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANhCGQ+EDnX0m9pzZAQE7iwF/VLbc2g6Ngwsucx/ZqE7uCNHYmPv7CAEy SrSNG+c7ggn5Xm20XQC6xhalWMjW1z8C =ZQae
User-Agent: tin/pre-1.4-980618 (UNIX) (HP-UX/B.10.20 (9000/778))
Newsgroups: comp.std.c++

Siemel Naran <sbnaran@localhost.localdomain> wrote:
: On 26 Sep 1998 18:15:29 GMT, Joe Keane <jgk@jgk.org> wrote:

:>C string functions don't allow null pointers either, so i don't see why
:>people should be surprised by the String class behavior.

: Well, you can do "strlen(0)", but you get a segmentation fault
: (ie, a program crash).

Although I agree with both of these comments, there is some difference
between a normal function and a constructor.  I expect constructors
to be very bullet proof.  At least it should throw an exception, but
not be left entirely undefined.  I do not expect that every "function"
in the strings library handle NULL.(or even any function)  Because
functions are called explicitly by the user it is obvious behavior
that it should crash if an unexpected input is applied.  

It is not obvious (to all users) that (my real) function crashed in
the typecasting.  At first, I thought that it was the gtk library or
the test code I was applying.  It took a while to track it back
to the constructor in the standard library.

: I guess it's for the efficiency.  If a func has to do a test
: "if (ptr==0) do_something1; else do_something2" you get slighly
: worse performance.  This is exacerbated by the fact that most
: strings are valid -- that is, ptr!=0 is #true -- so having to
: test for null explicitly is wasteful.

I agree as well.  However, constructors are special cases in
they may be called without explicit direction to do so.  Therefore,
they should have higher standards than a regular function.  All
behavior should be defined for a constructor.

: Also, the code looks clumsy when cluttered up with all those 'if'
: statements.

It would be a total of three if's in inline code, and there is 
no else involved.  

current implementation (gcc):
  basic_string (const charT* s)
    : dat (nilRep.grab ()) { assign (s); }

desired implementation:
  basic_string (const charT* s)
    : dat (nilRep.grab ()) { if (s) assign (s); }
  

--Karl
---
[ 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              ]



