comp.lang.ada
 help / color / mirror / Atom feed
From: "Marin David Condic" <marin.condic.auntie.spam@pacemicro.com>
Subject: Re: Some problems with string type(Exact String?)
Date: Wed, 6 Jun 2001 18:08:41 -0400
Date: 2001-06-06T22:08:42+00:00	[thread overview]
Message-ID: <9fm9la$c93$1@nh.pace.co.uk> (raw)
In-Reply-To: lowT6.40598$DG1.6446489@news1.rdc1.mi.home.com

In Ada, a string is just an array of characters - the array bounds have to
be fixed for an object of type String. The string you declare is thus always
N elements big. This is true of C as well. Any arrays of characters you
allocate have some fixed length for their lifetime. Referencing something
outside of their length is not checked by C - hence instead of an exception,
you get an error that may or may not kill the program on any given day of
the week and when it dies mysteriously, you get to haul out your debugger.

By *convention* (and ONLY by convention) when you have an array of char in
C, you stick null characters into the unused portions and all string
handling routines look for this as an indicator of the end of the string.
You could just as easily write a bunch of string routines in Ada that
depended on null termination. Its just that there are better ways.

When using fixed length strings, remember that the attributes 'First, 'Last,
'Range, etc. apply to the *array* and not to the *content* of that array. If
you want strings with more C-ish behavior look at the package
Ada.Strings.Bounded. This is essentially a record with a string element of
some maximum length and a current "length" element telling you where you
stopped storing characters in it. Hence they have a maximum size, but will
give you subprograms to work with the content. If you want to avoid the
whole mess - go to Ada.Strings.Unbounded and use that. This will have some
version of dynamic memory allocation to satisfy the current size of whatever
you store there. Its more expensive time-wise, but the size is always
whatever the contents is.

You *could* do what you suggest as far as padding - but in my experience,
its just a lot easier to convert anything of type String into something of
type Unbounded_String and use all the stuff you have in
Ada.Strings.Unbounded to manipulate the contents.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/

"McDoobie" <someone@nospam.net> wrote in message
news:lowT6.40598$DG1.6446489@news1.rdc1.mi.home.com...
>
> Alright, so in this example, the string has to be EXACTLY 20 characters.
>
> Now, in a couple other languages I've used, the string can be String<20
> characters but  not 20<String, rather than having to be String=20. So if I
> understand correctly, a string (or  more appropriately the elements of an
> ARRAY) have to precisely match the declared  bounds of the array. Yes, no?
>
> I'm currently reading through the "Arrays and Records" chapter of
> "Programming in Ada95" by Barnes. It's throwing alot of information at me,
> and I'm hitting a couple kinks in digesting  it all. But from what I can
> glean, in Ada95 arrays are fixed. Unlike C where I could go less  than but
> not greater than the bounds of the array.  My first hunch on the way to
> work with this strictness(or so it seems) would be keep count of the
> number of elements (characters of a string, numbers, whatever) going into
> the array, and then pad the rest of the array with with NULLs or something
> similar when there is no more  information to add to the array.
>
> Am I thinking along the right track here? Or am I missing something
> important?
>
> McDoobie chris@dont.spam.me





  parent reply	other threads:[~2001-06-06 22:08 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-07  2:18 Some problems with string type Valdes
2001-06-06 16:34 ` Philip Anderson
2001-06-06 20:29   ` Some problems with string type(Exact String?) McDoobie
2001-06-06 21:30     ` tmoran
2001-06-07  8:07       ` Jean-Pierre Rosen
2001-06-06 22:08     ` Marin David Condic [this message]
2001-06-07  6:48     ` Ada.Strings.Fixed procedure Move Petter Fryklund
2001-06-07  4:06   ` Some problems with string type Valdes
2001-06-07 13:20     ` Valdes Laurent
2001-06-06 17:49 ` tmoran
2001-06-07  4:07   ` Valdes
replies disabled

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