[fpc-pascal]StrAlloc and StrDispose of pChars and the like

Cox, Stuart SRM:EX Stuart.Cox at gems1.gov.bc.ca
Fri Nov 9 17:05:31 CET 2001


I'd appreciate help on this newbie, dinosaur question.

pChar strings seem like a wonderful extension of the 255 char limit that
Borland imposed when they originally designed TP.  Despite repeated attempts
to wring out bugs in my code I still seem to leave memory holes and have
heap related crashes.

How does BP7 know how much memory to release when it comes time to free
memory for a pChar string?  The BP7 help says the following things:

"StrDispose disposes of a string that was previously allocated with StrNew.
If Str is nil, StrDispose does nothing."

"StrNew allocates a copy of Str on the heap.  If Str is nil or points to an
empty string, StrNew returns nil and does not allocate any heap space.
Otherwise, StrNew makes a duplicate of Str,
obtaining space with a call to the GetMem standard procedure, and returns a
pointer to the duplicated string.  The allocated space is StrLen(Str) + 1
bytes long."

On the other hand, Delphi 1's help file says:

"The StrDispose procedure disposes of a string on a heap that was previously
allocated with StrNew.  If Str is nil, StrDispose does nothing."

"StrAlloc. This function allocates a buffer for a null-terminated string
with a maximum length of Size - 1 (1 byte must be reserved for the
termination character).  The maximum value of Size is 65,526. The result
points to the location where the first character of the string is to be
stored. A 16-bit number giving the total amount of memory allocated is
stored in the two bytes preceding the first character; it is equal to Size +
2. If space for a string is allocated with StrAlloc, it should be
deallocated via StrDispose.  StrAlloc is used by NewStr and is a general
purpose routine."

Are these two products, BP7 and Delphi 1, talking about the same TYPE?  Does
BP7 allocate a word sized memory allocation size indicator in memory
preceding the actual location of the first char of the pChar string?  If
not, how does the humble programmer release the same amount of memory as he
allocated to hold a long string that has become shorter during program
operation?

Thanks




More information about the fpc-pascal mailing list