comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <nickroberts@blueyonder.co.uk>
Subject: Re: Questions
Date: Fri, 25 Apr 2003 17:57:38 +0100
Date: 2003-04-25T17:57:38+01:00	[thread overview]
Message-ID: <b8bpdp$8a20o$1@ID-25716.news.dfncis.de> (raw)
In-Reply-To: wcc7k9mi4rb.fsf@shell01.TheWorld.com

"Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message
news:wcc7k9mi4rb.fsf@shell01.TheWorld.com...

> Jakub Zawadzki <darkjames@poczta.onet.pl> writes:
>
> > It's guestions from my friend
> > Emaix>I have a question. Does a String(1..X) occupy X bytes always, no
> > Emaix> matter what actual length the string has got?
>
> Heh?  The length of the string *is* X.  So yes, it occupies X bytes
> (plus whatever dope is necessary).

Sometimes a srting such as, for example, defined by String(1..10), will
occupy 20 or 40 bytes (plus 'dope information'), unless pragma Pack is
applied to it. This is because normally the compiler will allocate one
'storage element' per character (so as to maximise speed of access to it)
and on some target machines storage elements are 16 or 32 bits in size.
(Some unusual machines have other sizes of storage element.)

The 'dope' information effectively includes the bounds of the array (the 1
and the 10 in my example), but not necessarily in that form. Sometimes dope
the information has to be stored in memory, sometimes it doesn't. Often the
dope information is not stored in the same place as the array contents (the
characters), and often things are done with the dope information which are
not done with the contents. I'm afraid the full story about dope information
is very complex.

A string such as defined by String(1..10) is called a 'fixed-length' string.
Generally it is considered to have the bounds 1 and 10 (and so length 10)
permanently. However, sometimes the 'actual length' of a fixed-length string
is considered to be all the characters in the string minus any trailing
spaces (or NULs). In which case the answer, in essence, to the original
question is "yes": if I declare

   My_Name:  String(1..10) := "Nick      ";
   His_Name: String(1..10) := "Robert    ";

then the contents of My_Name will normally occupy the same amount of space
in memory as His_Name, even though the actual length (as just defined) of
My_Name is 4 and of His_Name is 6. The compiler simply stores the trailing
spaces as ordinary characters.

> >...Second: do
> > Emaix>To_String and To_Unbounded_String conversions copy strings
>
> Probably.

To expand on that reply a little, for both operations
(Ada.Strings.Unbounded_Strings.To_String and
Ada.Strings.Unbounded_Strings.To_Unbounded_String):

* Some implementations may only copy a pointer (and may also need to update
certain internal counters), but this will be rare.

* Most of the time there will be a simple copy of all the contents (the
characters) of the source string.

* In some cases it may be necessary to do more than just copying. For
example, sometimes it will be necessary to 'pack' or 'unpack' the
characters, as well as copying them.

For To_Unbounded_String, most implementations will need to do two things:
firstly, allocating a space on 'the heap' (and updating a pointer to it);
secondly, copying the contents (characters) of the source string into that
space.

I hope this is of some help!

--
Nick Roberts
Jabber: debater@charente.de [ICQ: 159718630]






  reply	other threads:[~2003-04-25 16:57 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-22 17:20 Questions Jakub Zawadzki
2003-04-22 19:59 ` Questions Stephen Leake
2003-04-22 21:47 ` Questions Robert A Duff
2003-04-25 16:57   ` Nick Roberts [this message]
2003-04-27 16:02     ` Questions Robert A Duff
2003-04-28 14:20       ` Questions Nick Roberts
     [not found] <mailman.0.1051099700.13478.comp.lang.ada@ada.eu.org>
2003-04-23 17:18 ` Questions tmoran
  -- strict thread matches above, loose matches on Subject: below --
1999-11-11  0:00 Questions Nobat
1999-11-11  0:00 ` Questions Tucker Taft
1999-11-11  0:00   ` Questions nobat
1999-11-11  0:00   ` Questions nobat
1999-11-11  0:00   ` Questions David Botton
1991-11-05 12:58 Questions Hugh Dunne
replies disabled

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