GPC Strings

Frank Heckenbach frank at g-n-u.de
Thu Jan 6 01:46:53 CET 2000


"Larry Carter" wrote:

> Hi everyone,
> 
>    I apologize if this subject has been discussed already, but since I could
> find no mention of it on either the GPC to-do pages or in the mailing list
> archives I thought that I would bring it up.
> 
>    I know that the current GPC string standard has no length limitation, but
> do you have to define the max length when declaring varaiables?  And if so,
> why?  One nice thing about Delphi's "AnsiStrings" is that they are
> dynamically assigned their length based on the length of the string
> assigned.  Also, they are reference counted so that copies of a string do
> actually take up memory.
> 
>    The reason that I am wondering about this is that I am writing a small
> unix utility that needs to parse file names (with path in some cases) and so
> I need strings long enough to accomodate these names.  However, I don't want
> to be wasting memory declaring for example,
> 
>    strWork : String(2048);
> 
> just to hold "HelloWorld.c" or even "/home/user/HelloWorld.c" but still be
> able to take care of those evil ones like
> "/usr/X11R6/lib/X11/shared/icons/samples/commercial/YourFriendlyNeighborhood
> ComputerGuy.txt" as an example (ok, a stupid one, but...).
> 
> 
>    Is there a way to do this with existing GPC strings?  If not, what about
> supporting Delphi's string type that would allow
> 
>    strWork : String;
> 
> and still accomodate any string needs up to available memory?

This is planned ("undiscriminated strings" on the To-Do list), but
currently we don't have the time to implement it, so it will have to
wait... :-(

Currently, the only way to do what you want is to use pointers and
allocate memory dynamically as you need it. You can do:

var
  p : ^String; { without length limitation }

[...]

New (p, 42); { allocate space for a string of length 42 }

p^ := 'some text which is no longer then 42 chars';

[...]

Dispose (p);

Frank

-- 
Frank Heckenbach, frank at g-n-u.de, http://fjf.gnu.de/
GPC To-Do list, latest features, fixed bugs:
http://agnes.dida.physik.uni-essen.de/~gnu-pascal/todo.html




More information about the Gpc mailing list