comp.lang.ada
 help / color / mirror / Atom feed
From: Dave Thompson <david.thompson1@worldnet.att.net>
Subject: Re: Getting length of array from C
Date: Mon, 07 Feb 2005 06:55:42 GMT
Date: 2005-02-07T06:55:42+00:00	[thread overview]
Message-ID: <tp3e01ptnnbnqppssvd6k3g1eqp7sti9jd@4ax.com> (raw)
In-Reply-To: 87lla8doyb.fsf@insalien.org

On Tue, 01 Feb 2005 21:07:08 +0100, Ludovic Brenta
<ludovic.brenta@insalien.org> wrote:
<snip>
> This reminds me of the C library's null-terminated strings.  Of
> course, all the functions in the C library that fail to use a length
> parameter are deprecated (e.g. strdup is deprecated in favour of
> strndup).
> 
Presumably you mean strncpy vs strcpy and strncat vs strcat, and the
wide-string wcs* versions similarly. These latter are not deprecated
by any C standard (yet), and the former are NOT just exact
replacements-plus-size for the latter -- this is a fairly frequent
source of problems posted to comp.lang.c. Neither is sprintf
deprecated in favor of snprintf, which is only standard as of C99.

(If you want the gritty, *ncpy pads if short but doesn't terminate if
long; *ncat always terminates but requires the _available/remaining_
size minus 1 not the total size. Oh, and snprintf was fairly widely
available pre-C99 but often with a different return value for the
overflow case. Oh joy.)

There is work in WG14 (and J11) currently on a proposed "TR"
(effectively a standardized option) for "secure" versions of library
routines that are exact replacements. If this is widely adopted it
might become required and the older forms deprecated around '09 or so.
Berkeley strlcpy and strlcat already are exact replacements, which
some people promote as preferable, but they are not standard, not even
POSIX/SUS.

To be clear, even in the 'n' 'l' and 's' versions only the size aka
maximum length (or a variant of it) is an added/explicit parameter;
the _current_ length is still done by NUL-termination. If you want
explicitly counted strings you have to build them yourself with mem*,
although of course in C++ you can package the result as classes with
methods and if you want operators -- or just use std::string which is
already pointer+counts like Ada Unbounded_String.

strdup is not in any C standard, although it is POSIX/SUS; strndup
doesn't exist at all and probably doesn't need to since strdup's
target is newly allocated large enough space not an existing buffer.

> You should look for a design document that explains where the array
> ends; there may be a terminating element at the end of the array, or
> perhaps the array has a "well-known size" somewhere.  If My_Array_Type
> is constrained, you're in luck because you'll know the size at compile
> time.

- David.Thompson1 at worldnet.att.net



  reply	other threads:[~2005-02-07  6:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-01  4:16 Getting length of array from C Chuck
2005-02-01  5:26 ` Simon Wright
2005-02-01  7:25 ` Martin Krischik
2005-02-01 13:17   ` Chuck
2005-02-01 14:17     ` Martin Krischik
2005-02-01 20:07     ` Ludovic Brenta
2005-02-07  6:55       ` Dave Thompson [this message]
2005-02-02  0:15     ` Jeffrey Carter
2005-02-02  2:22     ` Steve
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox