Kamran
Kamran

Reputation: 387

Get size of a pointer buffer in delphi

I want to get the size of a WriteFile Buffer to know how much data I must write in a buffer. the buffer datatype is Pointer Buffer:Pointer, i try to use SizeOf(Buffer) or SizeOf(@Buffer) but SizeOf does not return size of buffer received by 'WriteFile' , it simply return size of 'Pointer' data type (8).

what should i do ?

{Excuse me for my bad English}

Upvotes: 0

Views: 2105

Answers (1)

David Heffernan
David Heffernan

Reputation: 613491

You cannot retrieve the size of a buffer if all you have is a pointer to it. You must keep track of the buffer size independently of the pointer.

A common way to do this is to hold the size in a separate variable that you store along with the pointer. Pass the size on to any function that needs it.

Another way to do this is to use a dynamic array. The compiler and runtime keep track of the length of dynamic arrays automatically, and this can be queried using Length. You can get a pointer to the buffer with a simple cast:

var
  Buffer: TBytes; // dynamic array of byte
....
Buffer := ...; // initialize
WriteBuffer(Pointer(Buffer), Length(Buffer));

Upvotes: 4

Related Questions