Streams

From: Mark D. Lincoln (mdlincoln_at_speakeasy.net)
Date: 12/29/03


Date: Mon, 29 Dec 2003 17:51:16 -0500

All,

It seems that allocating a buffer for a stream to read to and and write from
is a bit strange. Can someone explain why one might do the following:

   var
      oStream : TMemoryStream;
      sStream : string;

   begin

      oStream.Position := 00;

      SetLength( sStream, self.Size );

      oStream.Read( Pointer( sStream )^, self.Size ); // Note the use of the
Pointer cast

   end;

verus the following:

   var
      oStream : TMemoryStream;
      sStream : string;

   begin

      oStream.Position := 00;

      SetLength( sStream, self.Size );

      oStream.Read( sStream[ 01 ], self.Size );

   end;

Why use the Pointer( sStream )^ in one case and the sStream[ 01 ] in
another. What is the difference? Is one better, faster, or more stable
than the other? This also seems to apply to the Write() method as well. I
have seen both of the following:

   oStream.Write( Pointer( sWrite )^, Length( sWrite ) );

and:

   oStream.Write( sWrite[ 01 ], Length( sWrite ) );

It has been been my experience with this one that the former Write() is more
stable that the latter if the string is null since a null string causes a
nasty access violation when trying to work with sWrite[ 01 ]. Can anyone
shed some more light on this?

            Mark



Relevant Pages

  • Re: someone an idea how i can optimize this a bit more
    ... string it has to create a new string and destroy the old one. ... even a million but it looks like the higher this var gets the slower the app ... GB of physical memory that is free on the machine (it does take 99% CPU ... also strange that systems is still a bit responsive but i see this as a pro ...
    (microsoft.public.dotnet.languages.vb)
  • Re: What is the best way to do this in Delphi?
    ... record will consist of a String and an array of integers. ... strings" to write in the file (consisting of a 4-byte integer followed ... Stream: TFileStream; ...
    (alt.comp.lang.borland-delphi)
  • Bug found in TstringList
    ... TStringList tries to split up the string that it loaded into multiple ... procedure TStrings.LoadFromStream(Stream: TStream); ... Stream: TStream; ...
    (alt.comp.lang.borland-delphi)
  • Re: TStringGrid
    ... >{writes a string to the stream} ... (Curiousity - not criticism) ...
    (alt.comp.lang.borland-delphi)
  • Re: Calculate the string statement
    ... verkn:(vkn:char; ... funkt:(fkt:string; ... var fkt,dfkt: p; ... var fehler: boolean; ...
    (comp.lang.pascal.borland)