[fpc-pascal] How to implement a circular buffer object in pascal?

Martin Frb lazarus at mfriebe.de
Thu Sep 3 18:17:31 CEST 2020


On 03/09/2020 17:44, Martin Frb via fpc-pascal wrote:
> type
>   TMyData = class
>       FData: array of byte;
>    end;
>   TMyThreadedQueue = specialize TLazThreadedQueue<TMyData>;
>
> Of course you can do
>   TMyData = array of byte;  // or record .... end if you want
>   TMyThreadedQueue = specialize TLazThreadedQueue<TMyData>;

Keep in mind, that if you push an array (or anything containing an 
array), you push a reference.

That is any modification you make to the array content after you pushed, 
will modify the date in the queue. (reference to the same data)

To prevent that you must ensure the data has only one reference holder.
See SetLength
https://www.freepascal.org/docs-html/rtl/system/setlength.html


More information about the fpc-pascal mailing list