[fpc-pascal]StrAlloc and StrDispose of pChars and the like
Cox, Stuart SRM:EX
Stuart.Cox at gems1.gov.bc.ca
Wed Nov 14 17:28:48 CET 2001
The implementation detail is important however. I purchased the RTL source
for BP7 when it came out and then was given the same code with the Delphi 1
There is no StrAlloc function in the BP7 RTL. The only allocation function
that Borland supplied was the StrNew function which looked at the current
length of an existing pChar then allocated and copied it to create a
duplicate of the original as it exists at the current moment. The memory
allocated to hold the original's original length was never consulted.
The net result is that, using the Borland supplied functions, one can only
create shorter and shorter copies and cannot lengthen pChar strings.
If one is to create pChars you really need to allocate an array of char just
like the example that Borland supplies with the introduction to pChars in
the BP7 manual. They don't mention any invisible allocation word as being
part of their implementation.
Without the programmer being told of the existence of the leading invisible
allocation word, he's at a loss as to how long the memory buffer was first
make. How long the string could become or how much memory is being used up.
Additionally, the StrDispose function, as written in the RTL, does not
consult any invisible allocation word but rather just FreeMems the memory
occupied by the pChar's current length plus the NUL.
I can't see how this combination of functions and the lack of any indicator
within the data itself of the initial length of the memory allocation can
possibly result in reliable programs.
Did Borland see the light with Delphi 1 and correct an oversight? Does Free
Pascal follow the BP7 or Delphi 1 model?
Map Generalization Technician, not
Forest Inventory and Monitoring Branch
Ministry of Sustainable Resource Management
email Stuart.Cox at gems1.gov.bc.ca
View the FMIB Website at:
From: Rich Pasco [mailto:pasco at acm.org]
Sent: Tuesday, November 13, 2001 4:23 PM
To: fpc-pascal at deadlock.et.tudelft.nl
Subject: Re: [fpc-pascal]StrAlloc and StrDispose of pChars and the like
"Cox, Stuart SRM:EX" wrote:
> Are these two products, BP7 and Delphi 1, talking about the same TYPE?
> BP7 allocate a word sized memory allocation size indicator in memory
> preceding the actual location of the first char of the pChar string?
That's an implementation detail you don't really need to know about.
As long as you pass the same pchar to StrDispose that you got from
StrAlloc, everything should work out.
> not, how does the humble programmer release the same amount of memory as
> allocated to hold a long string that has become shorter during program
I don't think that you're supposed to edit these strings in place on
the heap, certainly not truncate them by poking nulls into them.
fpc-pascal maillist - fpc-pascal at lists.freepascal.org
More information about the fpc-pascal