toStringz or not toStringz

Steven Schveighoffer schveiguy at yahoo.com
Wed Jul 13 11:31:42 PDT 2011


On Wed, 13 Jul 2011 13:32:56 -0400, Regan Heath <regan at netmail.co.nz>  
wrote:

> On Wed, 13 Jul 2011 17:00:39 +0100, Steven Schveighoffer  
> <schveiguy at yahoo.com> wrote:

>> How does your proposal know that a char * is part of a heap-allocated  
>> array?  If you are assuming the only case where char * is passed will  
>> be arr.ptr, then that doesn't cut it.  What if the compiler doesn't  
>> know where the char * came from?
>
> See your Q and my A above ("char * foo" example).
>
>> The inherent problem of zero-terminated strings is that you don't know  
>> how long it is until you search for a zero.  If it's not properly  
>> terminated, then you are screwed.  That problem cannot be "solved",  
>> even with compiler help -- you can get situations where there is no  
>> more information other than the pointer.
>
> Really?  But cant we obtain the GC lock and look them up, as mentioned  
> above?  And isn't this exactly what toStringz will do when the  
> programmer first of all curses because it has crashed, and then adds an  
> explicit toStringz call?

Who said the char * points into GC memory?  It could point at stack  
memory, or static data in ROM.

-Steve


More information about the Digitalmars-d mailing list