how to use strings/arrays of variable length?
Author Message
how to use strings/arrays of variable length?

How can I use arrays and strings without specifying the length in the

Like this way:
VAR
p: ^char;
n:byte;
...
BEGIN
n:=12;
p:=new(string[12]);
...

Greetz,

Wed, 18 Jun 1902 08:00:00 GMT
how to use strings/arrays of variable length?

Quote:

> How can I use arrays and strings without specifying the length in the

> Like this way:
> VAR
> p: ^char;
> n:byte;
> ...
> BEGIN
> n:=12;
> p:=new(string[12]);
> ...

> Greetz,

All that I can see above tells me: please stay programming in "C". It is

Franz Glaser
http://members.eunet.at/meg-glaser

Wed, 18 Jun 1902 08:00:00 GMT
how to use strings/arrays of variable length?

Contrary to Franz's complaint, yes there is a way, and you're close.

Quote:
> How can I use arrays and strings without specifying the length in the

> Like this way:
> VAR
> p: ^char;
> n:byte;
> ...
> BEGIN
> n:=12;
> p:=new(string[12]);
> ...

> Greetz,

GetMem( p, 13 );

This allocates 13 bytes of space pointed to by P. If you have a 12
character string, the 13th character gets a Null byte (chr(0)) which marks
the end of the string in memory. Read up on PChars and study the examples.
I find them to be very useful and flexible, once I got past my C-phobia.
You can do lots of things that you just can't do with regular Pascal
strings. And so what if it's not "real Pascal". A lot of Borland Pascal
isn't.

Cheers, Todd

P.S. It's more practical to learn the approaches given by several computer
languages, rather than stick with the single mindset provided by only one.

Wed, 18 Jun 1902 08:00:00 GMT
how to use strings/arrays of variable length?

Quote:

> How can I use arrays and strings without specifying the length in the

> Like this way:
> VAR
> p: ^char;
> n:byte;
> ...
> BEGIN
> n:=12;
> p:=new(string[12]);
> ...

Try this:

type pstring = ^string;

...

function newstr(n:byte):pstring;
var p:pstring;
begin
getmem(p,n+1); (* +1 for length byte *)
newstr:=p;
end;

procedure disposestr(var p:pstring);
var n:byte;
begin
if p = nil then exit;
n:=length(p^);
freemem(p,n+1);
p:=nil;
end;

It can be very dangerous, though, as the compiler won't check any
longer for string limits.  Code will assume p^ is really a string[255].
But the trick works ok if used cautiously.

--
"There ain't no such thing as a free lunch."

Wed, 18 Jun 1902 08:00:00 GMT
how to use strings/arrays of variable length?

Quote:
>Try this:

>type pstring = ^string;

>...

>function newstr(n:byte):pstring;
>var p:pstring;
>begin
>  getmem(p,n+1); (* +1 for length byte *)
>  newstr:=p;
>end;

Why not make:

function newsstr(st:string):pstring;
var p:pstring;
begin
getmem(p,length(st)+1);
p^:=st;
newstr:=p;
End;

You could also save memory by treating empty strings as special cases:

const empty:string[1]='';

Newstr:

begin

else begin...

end;

Of course one should also make a special case in disposestr:

(One could also return nil for empty strings but that would require
special handling when the string is accessed.

Quote:

>procedure disposestr(var p:pstring);
>var n:byte;
>begin
>  if p = nil then exit;
>  n:=length(p^);
>  freemem(p,n+1);
>  p:=nil;
>end;

>It can be very dangerous, though, as the compiler won't check any
>longer for string limits.  Code will assume p^ is really a string[255].
>But the trick works ok if used cautiously.

Things are safe as long as one does not change the length of such
strings while they are in place. Instead one should deallocate and
allocate a new string. Doing operations that do not change the string,
like capitalizing the string are OK.

Osmo

Wed, 18 Jun 1902 08:00:00 GMT
how to use strings/arrays of variable length?

Quote:

> Why not make:

> function newsstr(st:string):pstring;
> ...

Sure, I use most often this sort of newstr(string), a la C's strdup().
My answer using a newstr(byte) was in response to the previous poster
who was asking for a new string variable assuming a size, not a copy
of an existing string.

Quote:
> You could also save memory by treating empty strings as special cases:
> const empty:string[1]='';
> ...

>   else begin...
> ...
> Of course one should also make a special case in disposestr:

Interesting point.  Thanks for the idea.

Quote:
> >It can be very dangerous, though, as the compiler won't check any
> >longer for string limits.  Code will assume p^ is really a string[255].
> >But the trick works ok if used cautiously.

> Things are safe as long as one does not change the length of such
> strings while they are in place. Instead one should deallocate and
> allocate a new string. Doing operations that do not change the string,
> like capitalizing the string are OK.

Yes.  Agreed.  Any operation is ok as long it does not alter the length
so that no adjacent memory be overwritten and so that later disposestr
is able to free the right amount of memory.

--
"Sir, it's very possible this asteroid is not stable."

Wed, 18 Jun 1902 08:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages