To Walter, about char[] initialization by FF

Unknown W. Brackets unknown at simplemachines.org
Wed Aug 2 00:38:52 PDT 2006


Why would I ever use strncat() in a D program?

Consider this: if you do not wear a helmet while riding a motorcycle 
(read: I don't like helmets) you could break your head and die.  Guess 
what?  I don't ride motorcycles.  Problem solved.

I don't like null terminated strings.  I think they are the root of much 
evil.  Describing why having 0 as a default benefits null terminated 
strings is like describing how having less police help burglars to me. 
Obviously I'm being over-dramatic, but I remain unconvinced...

Also I did spend (some of) my childhood in Boy Scout camps and I did 
learn many principles (none of which related to programming in the 
slightest.)  I mean that literally.  But you're right, that's beside the 
point.

-[Unknown]


>> But maybe that's because I never leave things at their defaults.  It's
>> like writing a story where you expect the reader to think everyone has 
>> brown eyes unless you say otherwise.
>>
> 
> Consider this:
> 
> char[6] buf;
> strncpy(buf, "1234567", 5);
> 
> What will be a content of you buffer?
> 
> Answer is: 12345\xff . Surprise? It is.
> 
> In modern D reliable implementation of this shall be as:
> 
> char[6] buf; // memset(buf,0xFF,6); under the hood.
> uint n = strncpy(buf, "1234567", 5);
> buf[n] = 0;
> 
> if you are going to use this with non D modules.
> 
> Needless to say that this is a bit redundant.
> 
> If D in any case initializes that memory why you need
> this uint n and buf[n] = 0; ?
> 
> Don't tell me please that this is because your spent
> your childhood in boyscout camps and got some high principles.
> Lets' put aside that matters - it is purely technical discussion.
> 
> Andrew.



More information about the Digitalmars-d mailing list