Re: Let's make it backwards compatible :D
From: Skybuck Flying (nospam_at_hotmail.com)
Date: 02/21/04
- Next message: Skybuck Flying: "Re: Initializing/Finalizing each field might be possible too :D"
- Previous message: AlanGLLoyd: "Re: midistreamopen"
- In reply to: Rob Kennedy: "Re: Let's make it backwards compatible :D"
- Next in thread: Skybuck Flying: "Re: Let's make it backwards compatible :D"
- Reply: Skybuck Flying: "Re: Let's make it backwards compatible :D"
- Reply: Skybuck Flying: "Logic, logic, logic :D"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 21 Feb 2004 08:21:21 +0100
> > // step 1 set a string
> > procedure TForm1.Button1Click(Sender: TObject);
> > var
> > CrazyOld*** : ShortString;
> > begin
> > // initialize string first
> > Initialize( AnsiString(Storage) );
>
> Incorrect use of Initialize. The argument to Initialize should be a
> *pointer*, not a string. A string variable may *hold* a pointer, but it
> is not a pointer; it is a string. If you want to initialize something,
> use a PString. However, since the value of Storage is nil, Initialize
> would probably crash if you called Initialize with a correctly typed
> parameter. (My guess is that the compiler knows that a string by itself
> needs no initialization, so it doesn't generate any code for that line.)
Hmm,
I was hoping initialize would create the little block that is necessary for
AnsiStrings.
The little block being: |Reference Count|String Length|Null Terminator|
The pointer storage would point to the null terminator.
Now you telling me that's not so, how depressing :D
But glad you are onboard :D
It seems initialize in the system.pas tries to set all members of a record
or string to zero, but it doesn't allocate the little block.
So calling initialize they way I did will just make the program try and put
zero's in some arbitrary memory.
>
> > CrazyOld*** := 'This is a string';
> >
> > AnsiString(Storage) := CrazyOld***;
>
> OK. Storage now holds a string reference. The reference count is 1.
Yes but shoulnd't the string first be allocated as you described in another
post.
New( PString(Storage) );
>
> > end;
> >
> > // step 2 get a string
> > procedure TForm1.Button2Click(Sender: TObject);
> > begin
> > EditStorage.Text := AnsiString(Storage);
>
> OK. The edit box now holds a copy of the string, in PChar form. The
> reference count of the string help by Storage is still 1.
>
> > end;
> >
> > // step 3 destroy the string
> > procedure TForm1.Button3Click(Sender: TObject);
> > begin
> > AnsiString(Storage) := '';
>
> OK. Storage will be adjusted to holds a different string value (nil, in
> this case), so the previous string will have its reference count
> decremented. The reference count should become 0, so the string's memory
> will be freed.
Hmm but shouldn't the little block be cleaned up with
Dispose( Pstring(Storage) );
>
> > // finalize string
> > Finalize( AnsiString(Storage) );
>
> As with Initialize above, I doubt that line actually generates any code
> at all.
>
> --
> Rob
- Next message: Skybuck Flying: "Re: Initializing/Finalizing each field might be possible too :D"
- Previous message: AlanGLLoyd: "Re: midistreamopen"
- In reply to: Rob Kennedy: "Re: Let's make it backwards compatible :D"
- Next in thread: Skybuck Flying: "Re: Let's make it backwards compatible :D"
- Reply: Skybuck Flying: "Re: Let's make it backwards compatible :D"
- Reply: Skybuck Flying: "Logic, logic, logic :D"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]