Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Re: What does this error mean?

2005-04-11 by Robert Adsett

At 12:21 PM 4/11/05 +0200, Paul Colin Gloster wrote:

>On Tue, Feb 22, 2005 at 02:21:51PM -0500, Robert Adsett wrote:
>
>"At 08:06 PM 2/22/05 +0100, Paul Colin Gloster wrote:
>[..]
>
> >The original ANSI C has an
> >inefficient library routine which was knowingly standardized as such
> >because many of the implementations of the function in draft 1980's ANSI C
> >were buggy to the point of gross ineffiency, even though this function had
> >originally been proposed with the motivation (and simple implementation of
> >being) efficient. And years after current ANSI C was standardized in 1999,
> >few ANSI C compilers have ever been released.
>
>library routine?  Block locals have been legal since pre-ANSI AFAIK and
>[..]"
>
>I was not thinking of declarations within a block. I was probably
>incompletely remembering the following thread "accu-general: Rationale
>for behaviour of strncpy() in C89/C99" on accu-general@ACCU.org :
>
>timestamped Wed, 03 Jan 2001 08:45:47 +0000
>an email from someone contained:
>
>"Hiya!
>
>I came across something recently which puzzled me slightly simple because I
>can't work out *why* the standard was written in the way it is.  Allow me
>to explain - consider the code:
>
>         char string[16];
>
>         memset(string, '*', sizeof(string));
>         strcpy(string, "Hi");
>         strncpy(string, "Hello World", sizeof(string));
>
>After the strcpy(),  string contains "Hi\0*************".  This seems quite
>reasonable - the minimum number of bytes have been changed to copy the
>string.
>
>After the strncpy(),  string contains "Hello World\0\0\0\0\0" - an extra 4
>bytes have been assigned values by this call,  despite the fact that this
>does not affect the representation of the string at all.  In this case the
>speed penalty is almost negligible,  however if the array had been declared
>somewhat larger,  it may become quite significant.

OK, that makes more sense, on the other hand there are a lot of other 
string and memory copy library routines without that behaviour and if they 
don't fit it's easy enough to write a routine that matches strncpy in every 
respect other than padding the string length with 0's.  Also as pointed out 
it wasn't a bug.  The original was introduced for directory manipulation.

Robert



" 'Freedom' has no meaning of itself.  There are always restrictions,   be 
they legal, genetic, or physical.  If you don't believe me, try to chew a 
radio signal. "  -- Kelvin Throop, III
http://www.aeolusdevelopment.com/

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.