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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,45b47ecb995e7a3 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-08-13 13:20:09 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cpk-news-hub1.bbnplanet.com!news.gtei.net!howland.erols.net!news-out.worldnet.att.net.MISMATCH!wn3feed!worldnet.att.net!135.173.83.71!wnfilter1!worldnet-localpost!bgtnsc06-news.ops.worldnet.att.net.POSTED!not-for-mail Message-ID: <3B783712.88029BB8@worldnet.att.net> From: James Rogers X-Mailer: Mozilla 4.76 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Ada Idioms Progress Preview References: <3B6F1B2F.4FC3C833@gsde.hou.us.ray.com> <5ee5b646.0108071819.6e84e33d@posting.google.com> <3_Xc7.45$NM5.84779@news.pacbell.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Mon, 13 Aug 2001 20:20:09 GMT NNTP-Posting-Host: 12.86.36.138 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc06-news.ops.worldnet.att.net 997734009 12.86.36.138 (Mon, 13 Aug 2001 20:20:09 GMT) NNTP-Posting-Date: Mon, 13 Aug 2001 20:20:09 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:11868 Date: 2001-08-13T20:20:09+00:00 List-Id: Ole-Hjalmar Kristensen wrote: > > One thing which can be said in favour of having a terminator character > is that it frees you from having to store the length explicitly. The > length of a string is usually different from the size of the array > used to store the string. > So, in a sense a C string is more self-describing than a plain Ada > string. > Of course, as soon as you call a procedure, you can use a slice, but > you still need the actual length to decide which slice. > > On the balance, I would rather have Ada strings. An old saying is "there is no free lunch". In other words, nothing comes for free. In the case of a C string, you do not explicitly carry around the length of a string. Instead, you rely on a convention stating that the logical end of the string is indicated by a null character. The C approach presents two very real costs: 1) You must serially read the string to find the terminating null character. This operation is very expensive if you only need to determine the length of the string. 2) Sometimes the null character is omitted. Since C arrays are unbounded, this causes your program to read beyond the end of the string until it finds a null character. The resulting length will be incorrect. When copying or editing a string this problem will result in data corruption and undefined behaviors. Another less common cost occurs when copying C strings. The most efficient copy operation for C arrays is the memcpy function. This function allows you to copy blocks of memory efficiently. If you try to use memcpy to copy strings you will find some real problems. In those cases you want to copy the actual array of characters, not just the logical string contained in it. The problem is that the C sizeof operator does not report the correct size of arrays outside the immediate scope where they are declared. Instead you will only get the size of the pointer to the first element of the array. Therefore, to efficiently copy C strings using memcpy you must provide a second "length" argument, which may not be readily available. Jim Rogers Colorado Springs, Colorado USA