Author Topic: [SOLVED] TMemoryStream Question with possible bug FPC 3.2.2  (Read 4884 times)

Schmitty2005

  • Jr. Member
  • **
  • Posts: 65
Re: TMemoryStream Question with possible bug FPC 3.2.2
« Reply #15 on: July 24, 2025, 04:16:02 am »
TMemoryStream  is backed only by a single block of memory (accessible via the aptly named  Memory  property), meaning that you can just call  FillChar / FillQWord / ZeroMemory  et al. on that block. You don't even need to reset the position since  Memory  always refers to the start of the block:

Code: Pascal  [Select][+][-]
  1. procedure ZeroStream(Stream: TMemoryStream);
  2. begin
  3.   FillChar(Stream.Memory^, Stream.Size, 0);
  4. end;

FillChar is exactly the one-liner I was looking for !  Thank you!


P.S.: You don't need to pass class instances as  var  in most cases; even with  const  class members may still be modified.
Class instances are essentially pointers, and class types don't get special treatment for  var / out / const / constref;  these specifiers still refer to the pointer value of a class instance and not the instance data.
Just FYI  :)
I am a novice, thank you for pointing that out as well.

Schmitty2005

  • Jr. Member
  • **
  • Posts: 65
Re: TMemoryStream Question with possible bug FPC 3.2.2
« Reply #16 on: July 24, 2025, 04:18:06 am »
In such a case I'd probably just use a TBytesStream (one of TMemoryStream descendants), built from a dynamic array. Dynamic arrays (as far as I remember), are guaranteed to be zero-initialized from the compiler. Like this:

Code: Pascal  [Select][+][-]
  1. function CreateStream: TBytesStream;
  2. var
  3.   Buffer: array of Byte;
  4. const
  5.   Samples = 44100 * 2 * 10; { 10 secs at 44khz 16-bit stereo }
  6.   SizeInBytes = Samples * 2;
  7. begin
  8.   SetLength(Buffer, SizeInBytes);
  9.   Result := TBytesStream.Create(Buffer);
  10. end;

I will do this in the future, as it seems to make more sense than what I was doing.  Thank you!

 

TinyPortal © 2005-2018