[DUG] Pointers help

Stefan Mueller muellers at orcl-toolbox.com
Sun Nov 5 15:24:48 NZDT 2017


Ross,

 

“array of bytes” isn’t the same as a “pointer” (nor is the same as a static
array with a fixed length).  Dynamic arrays are a managed data structure in
Delphi that contain information such as length and a reference count (so
Delphi can manage memory and free them if no longer used). Converting data
from a memory-pointer to a dynamic array isn’t possible by typecasting
alone, you will need to copy the data as in your existing code – Indies
idGlobal.pas contains a helper function which does the same as your code:
function RawToBytes(const AValue; const ASize: Integer): TIdBytes;

 

 

Kind regards,



Stefan Müller,
R&D Manager

ORCL Toolbox Ltd. 
Auckland, New Zealand 


P Please consider the environment before printing this email

This message is intended for the addressee named above and may contain
privileged or confidential information.
If you are not the intended recipient of this message you must not use,
copy, distribute or disclose it to anyone.

 

From: delphi-bounces at listserver.123.net.nz
[mailto:delphi-bounces at listserver.123.net.nz] On Behalf Of Ross Levis
Sent: Sunday, 5 November 2017 2:19 p.m.
To: delphi at delphi.org.nz
Subject: [DUG] Pointers help

 

I’m writing data to an Indy IOHandler by moving the data from a
TMemoryStream.Memory to a TIDBytes (array of byte) before writing it as I
cannot find how to do it otherwise.

 

This works...

 

AudioMS: TMemoryStream;

Buffer: TIdBytes; // array of byte

MemPos: PByte;

....

MemPos := AudioMS.Memory;

Inc(MemPos,FromPos);

ToPos := Player.AudioMS.Position;

SetLength(Buffer,ToPos-FromPos);

Move(MemPos^,Buffer[0],Length(Buffer));

IOHandler.WriteDirect(Buffer);

FromPos := ToPos;

 

 

But there has to be a better way of writing direct from the AudioMS.Memory
and getting rid of the Move operation.

 

I’ve tried the following but produces pointer errors or access violations.

 

IOHandler.WriteDirect(TIdBytes(MemPos),ToPos-FromPos);

 

Also tried...

IOHandler.WriteDirect(TIdBytes(MemPos^),ToPos-FromPos);

 

There is a IOHandler.Write function that supports TStream but the memory
stream is being appended to in a separate thread so I can’t go changing the
Position I want to access the data from. 

 

 

How should it be done?

 

Cheers,

Ross.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20171105/5a24387e/attachment.html 


More information about the Delphi mailing list