From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,91fb0c338516ed9 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.glorb.com!wn13feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail From: Dave Thompson Newsgroups: comp.lang.ada Subject: Re: Getting length of array from C Message-ID: References: <1107231381.627097.52490@c13g2000cwb.googlegroups.com> <1107244808.d76a000ed3c64143f06cacb757120823@teranews> <1107263868.447580.257310@z14g2000cwz.googlegroups.com> <87lla8doyb.fsf@insalien.org> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Mon, 07 Feb 2005 06:55:42 GMT NNTP-Posting-Host: 12.75.199.233 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1107759342 12.75.199.233 (Mon, 07 Feb 2005 06:55:42 GMT) NNTP-Posting-Date: Mon, 07 Feb 2005 06:55:42 GMT Organization: AT&T Worldnet Xref: g2news1.google.com comp.lang.ada:8177 Date: 2005-02-07T06:55:42+00:00 List-Id: On Tue, 01 Feb 2005 21:07:08 +0100, Ludovic Brenta wrote: > 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