toStringz or not toStringz
Regan Heath
regan at netmail.co.nz
Tue Jul 12 08:43:33 PDT 2011
On Tue, 12 Jul 2011 16:04:15 +0100, Steven Schveighoffer
<schveiguy at yahoo.com> wrote:
> On Tue, 12 Jul 2011 10:59:58 -0400, Steven Schveighoffer
> <schveiguy at yahoo.com> wrote:
>
>> On Tue, 12 Jul 2011 10:50:07 -0400, Regan Heath <regan at netmail.co.nz>
>> wrote:
>>
>>> and in both cases, toStringz would do nothing as foo is zero
>>> terminated already (in both cases), or am I wrong about that?
>>
>> In neither case are they required to be null terminated. The only
>> thing that guarantees null termination is a string literal. Even
>> "abc".dup is not going to be guaranteed to be null terminated. For an
>> actual example, try "012345678901234".dup. This should have a 0x0f
>> right after the last character.
>
> And, actually, the cost penalty of checking if you are going to segfault
> (i.e. checking if the ptr is into heap data, and then getting the
> length) is quite costly. You must take the GC lock.
I wouldn't know anything about this. I was assuming when toStringz was
called on a slice it would use the array capacity and length to figure out
where the \0 needed to be, and do as little work as possible to achieve
it. Meaning in most cases that \0 is written to 1 past the length, inside
already allocated capacity.
--
Using Opera's revolutionary email client: http://www.opera.com/mail/
More information about the Digitalmars-d
mailing list