comp.lang.ada
 help / color / mirror / Atom feed
From: James Rogers <jimmaureenrogers@worldnet.att.net>
Subject: Re: Ada Idioms Progress Preview
Date: Mon, 13 Aug 2001 20:20:09 GMT
Date: 2001-08-13T20:20:09+00:00	[thread overview]
Message-ID: <3B783712.88029BB8@worldnet.att.net> (raw)
In-Reply-To: umqr8ug55d9.fsf@maestro.clustra.com

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



  parent reply	other threads:[~2001-08-13 20:20 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-03  4:16 Ada Idioms Progress Preview James Rogers
2001-08-03 19:45 ` Robert Dewar
2001-08-03 22:02   ` James Rogers
2001-08-06 22:33   ` Stanley R. Allen
2001-08-07  2:45     ` tmoran
2001-08-07 12:15       ` Larry Kilgallen
2001-08-07 13:26         ` Philip Anderson
2001-08-08  2:23         ` Robert Dewar
2001-08-08  5:58           ` Ehud Lamm
2001-08-08  2:19       ` Robert Dewar
2001-08-08 15:13         ` Ted Dennison
2001-08-08 18:03           ` tmoran
2001-08-09 20:36           ` Florian Weimer
2001-08-10 21:02         ` Jay Nabonne
2001-08-10 21:51           ` Larry Kilgallen
2001-08-13 14:19             ` Ted Dennison
2001-08-13 14:05           ` Ted Dennison
2001-08-13 14:19             ` Marin David Condic
2001-08-13 15:47             ` Ole-Hjalmar Kristensen
2001-08-13 16:22               ` Marin David Condic
2001-08-13 18:48               ` Larry Kilgallen
2001-08-14  7:05                 ` Ole-Hjalmar Kristensen
2001-08-13 20:20               ` James Rogers [this message]
2001-08-14  1:09                 ` Warren W. Gay VE3WWG
2001-08-14  6:15                   ` James Rogers
2001-08-14 14:03                     ` Warren W. Gay VE3WWG
2001-08-21  5:54                   ` C strings, was " David Thompson
2001-08-16 18:42                 ` Jay Nabonne
2001-08-17  1:25                   ` Robert Dewar
2001-08-13 21:47               ` Ted Dennison
2001-08-14  7:37                 ` Ole-Hjalmar Kristensen
2001-08-14 14:59                   ` Ted Dennison
2001-08-14 13:22                 ` Marin David Condic
2001-08-14 15:12                   ` Ted Dennison
2001-08-14 15:33                     ` Marin David Condic
2001-08-14  8:49               ` Lutz Donnerhacke
2001-08-14  9:38                 ` Ole-Hjalmar Kristensen
2001-08-14  9:54                   ` Lutz Donnerhacke
2001-08-14 14:51                     ` James Rogers
2001-08-14 16:44                   ` Darren New
2001-08-14  1:39             ` Slicing ( Ada Idioms Progress Preview ) Warren W. Gay VE3WWG
replies disabled

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