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-16 11:42:02 PST Path: archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.stealth.net!newsfeed.nyc.globix.net!newsfeed.sjc.globix.net!cyclone-sf.pbi.net!206.13.28.33!news.pacbell.net.POSTED!not-for-mail From: "Jay Nabonne" Newsgroups: comp.lang.ada References: <3B6F1B2F.4FC3C833@gsde.hou.us.ray.com> <5ee5b646.0108071819.6e84e33d@posting.google.com> <3_Xc7.45$NM5.84779@news.pacbell.net> <3B783712.88029BB8@worldnet.att.net> Subject: Re: Ada Idioms Progress Preview X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Message-ID: Date: Thu, 16 Aug 2001 11:42:01 -0700 NNTP-Posting-Host: 63.194.203.138 X-Complaints-To: abuse@pacbell.net X-Trace: news.pacbell.net 997987321 63.194.203.138 (Thu, 16 Aug 2001 11:42:01 PDT) NNTP-Posting-Date: Thu, 16 Aug 2001 11:42:01 PDT Organization: SBC Internet Services Xref: archiver1.google.com comp.lang.ada:12005 Date: 2001-08-16T11:42:01-07:00 List-Id: "James Rogers" wrote in message news:3B783712.88029BB8@worldnet.att.net... > 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 A third occasional cost: 3) Strings that need to have null characters in them. It doesn't happen too often, but if (under MS Windows) you ever want to read printer information (the old way) or pass filter strings (e.g. "*.adb") to a file dialog, you have to go through contortions to get a "string" that works. -Jay Nabonne