From: Rex Reges <reges@mds.lmco.com>
Subject: Re: Need of help Leonid(dulman@ibm.net)
Date: 1997/02/12
Date: 1997-02-12T00:00:00+00:00 [thread overview]
Message-ID: <3302387F.16BB@mds.lmco.com> (raw)
In-Reply-To: u8n2tdm68y.fsf@ehmgs2.et.tu-dresden.de
Thomas Hoffmann wrote:
... deleted comments about types with large discriminant ranges ...
> To "solve" the problem yourself, just declare a reasonable maximum length, and use a subtype
> representing this length as the subtype of the discriminant:
>
> Max_Length : constant := 200;
>
> subtype Index is
> Natural range 0 .. Max_Length;
>
> type V_String (Size : Index := 0) is
> record
> S : String (1 .. Size);
> end record;
>
> GNAT 3.09 issues a warning during the compiler run.
I think it is a bad idea to have a warning issued for this example.
Even if warnings can be suppressed or ignored, it generally takes
a written description in the comments and approval by Quality
Assurance to do so.
I have used the V_String in the form of a generic package with
the Max_Length as the parameter. This allows the creation
of necessary functions as well: catenation of V_String and
V_String, catenation of String and V_String, etc.
From the V_String package I instantiate three versions:
Small_Variable_Length_String
Medium_Variable_Length_String
Large_Variable_Length_String
I would be unhappy trying to explain why the basis for most string
storage causes compiler warnings.
An interesting problem occurred while trying to provide the
utility functions. I had the following function:
function V_String_Of( Standard_String : in String )
return V_String is
begin
-- Test omitted for Strings that are too large to fit.
return ( Size => Standard_String'Length ,
S => Standard_String ) ;
end V_String_Of ;
I liked this code because it did not require creating a
temporary variable on the stack, particularly if the string
was very large. However, it failed whenever a substring
was passed which had a starting index other than 1:
My_V_String := V_String_Of( "abcdefg"(2:3) ); -- Not sure this
syntax
-- is allowed but you
get
-- the point.
The aggregation mechanism will not shift the indices, but the
assignment operator will. Therefore, I was forced to rewrite the
function using a temporary value on the stack:
function V_String_Of( Standard_String : in String )
return V_String is
Temp_String : String( 1..Standard_String'Length )
:= Standard_String ;
begin
-- Test omitted for Strings that are too large to fit.
return ( Size => Temp_String'Length ,
S => Temp_String ) ;
end V_String_Of ;
Another mechanism which causes an invisible temp variable is
to use the string catenation function to shift the indices.
function V_String_Of( Standard_String : in String )
return V_String is
begin
-- Test omitted for Strings that are too large to fit.
return ( Size => Standard_String'Length ,
S => "" & Standard_String ) ;
end V_String_Of ;
Both of these have the potential of blowing out the stack for large
strings, particularly in tasks with small stack allocations.
--
Rex Reges or you can call me The Fixer
Systems Analyst or you can call me The Lawyer
Lockheed Martin, M&DS or you can call me The Doctor
(610)354-5047 or you can call me Rexasaurus
next prev parent reply other threads:[~1997-02-12 0:00 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-02-08 0:00 Need of help Leonid(dulman@ibm.net) dulman
1997-02-08 0:00 ` Robert Dewar
1997-02-10 0:00 ` Thomas Hoffmann
1997-02-10 0:00 ` Robert Dewar
[not found] ` <5dpftl$qi@fozzie.sun3.iaf.nl>
1997-02-12 0:00 ` Robert Dewar
1997-02-13 0:00 ` Keith Thompson
1997-02-12 0:00 ` Rex Reges [this message]
1997-02-15 0:00 ` Keith Thompson
1997-02-17 0:00 ` Robert Dewar
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox