开发者

How to use this: array[0..x] of byte?

I have this piece of code. The BufferAddress was initially of PByteArray type. However, PByteArray is limited to 32768 elements but I need more than than.

I don't need at run time what is the maximum limit, so I defined TLargeArrayOfBytes from 0 to 2GB. There is a nicer way to do it?

function TBufferedStream.Read(VAR aBuffer; Count: Integer): Longint;
TYPE
   TLargeArrayOfBytes = array[0..2000000000] of Byte;
   PArrayOfBytes = ^TLargeArrayOfBytes;
VAR
   BufferAddress: PArrayOfBytes;       // it was PByteArray
begin
 ...

 { Then refill buffer... }
 FillReadBuffer;

 { ...and continue reading }
 BufferAddress:= @TLargeArrayOfBytes(aBuffer);
 Result:= Result+ ReadFromBuffer(BufferAddress[Result], Count- Result);  { Read from RAM buf开发者_StackOverflowfer }
end;

Delphi 7


I don't see anything wrong in your definition, the only suggestion is you can use the MaxInt const.

type
   TLargeArrayOfBytes = array[0..MaxInt-1] of Byte;

or maybe the SysUtils.TBytes type


PByteArray isn't really limited to that many bytes. It can point to an array of any size. The only problem comes if you have range checking enabled when you apply the bracket operator to the array pointer. Then the compiler checks that the index is within the bounds of the declared type, regardless of how many bytes the pointer really points at. Disable range checking before such an expression and then turn it back on afterward.

BufferAddress:= PByteArray(@aBuffer);
{$R-}
// Read from RAM buffer
Result := Result + ReadFromBuffer(BufferAddress[Result], Count - Result);
{$R+}


Using the POINTERMATH directive you don't need to declare types of pointer to array.

{$POINTERMATH ON}

procedure TForm1.FormCreate(Sender: TObject);
var
  P : PByte;
begin
  GetMem(P,1000);

  P^ := 42;      //Access first/current element
  P[999] := 42;  //Access last element

  FreeMem(P);
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜