Ross,
Hi. I am not giving the authoritive answer here, but here is what I can gleam from reading the manual and looking at source and compiled code:
Initially A & B both point to the same string. The string has a reference count of 2 - because two variables refer to it.
Once you assign A to something else, the reference count drops down to 1. When B is assigned something else, or when it goes out of scope, the reference count will drop to 0 and the memory manager will release it. Its not rocket science.
Another scenario: Suppose both A and B point to the same string. If you make a modification to A by using code like:
A[5] = 'Z';
What happens is behind the scenes, the compiler creates a copy of the string, sets the 5th position to 'Z' and changes A to point to the new string - "copy on write".
Ansi Strings as parameters.
Normal parameter (no consts, out or var): The pointer value is copied and passed to the function. The reference count on the string data is incremented. This is decremented when either the parameter is assigned to another value or when the function goes out of scope. Any modifications to the parameter causes the a new string to be created using the copy-on-write rules. The original string is not touched.
Var parameter: The pointer to the reference is passed to the function.
function Foo (var Bar: string); begin Bar := 'test'; end;
is equivalent to:
function Foo (Bar: PSstring); begin ^Bar := 'test'; end;
Reference count is not affected unless you assign the parameter to another value.
Out parameter: Same as var except the compiler generates code that clears out the string before the call and issues a warning if you try to read the parameter value without first assigning to it.
Const parameter: Behaves the same as normal except reference counting is not used. The compiler issues an error if you try to assign to the parameter.
Delphi does not copy Ansi strings before calling a function. However, passing as const saves the compiler from generating finalization code.
Dennis.
From: "Ross Levis" <[EMAIL PROTECTED]>
Hi Dennis
So if I do this:
var A,B: String; A := 'Hello'; B := A;
Are you saying that 'Hello' is only in one memory location? If I then do: A := 'There';
A is reallocated to new memory space and B still occupies the original location of A? Does the memory manager keep track of all string allocations pointing to the same address and when the last variable is assigned to a different string, it automatically releases the memory used by the original string?
That seems very complicated for a memory manager.
On a similar subject then, is sending string parameters in functions and procedures as const a waste of time. I was under the impression that sending a string parameter by value was sending a copy of the string.
Cheers, Ross.
----- Original Message ----- From: "Dennis Chuah" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, September 19, 2004 1:21 PM
Subject: Re: [DUG] TStringList.FList
Ross,
There is no need to store a pointer as a string in Delphi is a reference anyway. Behind the scenes, it is actually a pointer that points to the first byte of the string.
You cannot access the string with ^ because it is not a PString, but a string type.
Dennis.
From: "Ross Levis" <[EMAIL PROTECTED]>
Hi Dennis
Hi. If you looked at the definition of TStringItem, you will see that FString is of type string.
Yes, but I want to store a pointer to the memory location of the string. In fact, I'm storing the locations of all strings added to the stringlist. I don't want a copy of the strings as that would double memory requirements.
BTW, I still don't understand why you can't simply do stringlist.Strings[index] to get at the string.
The index will change as items are added because it's a sorted stringlist. I need access to the strings via other classes. But I also need a fast Find operation which the Stringlist provides. I hope that makes sense.
I still don't understand why I can't simply dereference the PString using a ^, which should convert it to a string.
Cheers, Ross.
_______________________________________________ Delphi mailing list [EMAIL PROTECTED] http://ns3.123.co.nz/mailman/listinfo/delphi
_________________________________________________________________ Need more speed? Get Xtra JetStream @ http://xtra.co.nz/jetstream
_______________________________________________ Delphi mailing list [EMAIL PROTECTED] http://ns3.123.co.nz/mailman/listinfo/delphi
_______________________________________________ Delphi mailing list [EMAIL PROTECTED] http://ns3.123.co.nz/mailman/listinfo/delphi
_________________________________________________________________
Don�t just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/
_______________________________________________ Delphi mailing list [EMAIL PROTECTED] http://ns3.123.co.nz/mailman/listinfo/delphi
