I am trying to convert a piece of code into Delphi but fall down when it comes to this example :

void __stdcall GetBuffers( short **xBuffers, unsigned long nValues)
{
.......
memcpy(TargetA, xBuffers[0], nValues * sizeof(short));
memcpy(TargetB, xBuffers[1], nValues * sizeof(short));
memcpy(TargetC, xBuffers[3], nValues * sizeof(short));
memcpy(Targetd, xBuffers[4], nValues * sizeof(short));
}

Could you please help

Dani AI

Generated

A short explanation and a couple of practical ways to map C++ short **xBuffers into Delphi.

C++ short** is “a pointer to an array of pointers”, where each pointer points at a buffer of 16‑bit integers. If you control both sides, the simplest, safest Delphi representation is a dynamic-array-of-dynamic-arrays (what suggested and what confirmed worked). That avoids manual pointer math and makes Move/array indexing straightforward.

If you must accept a short** from C/C++ (DLL boundary), treat it as an array of raw pointers and cast it to a typed pointer-array in Delphi, then copy each buffer with System.Move (or RtlMoveMemory) using SizeOf(SmallInt) for byte counts. Example pattern:

type
  PSmallInt = ^SmallInt;
  PPointerArray = ^TPointerArray;
  TPointerArray = array[0..(MaxInt div SizeOf(Pointer)) - 1] of Pointer;

procedure GetBuffers(xBuffers: Pointer; nValues: Cardinal); stdcall;
var
  ptrs: PPointerArray;
  src: PSmallInt;
begin
  ptrs := PPointerArray(xBuffers);

  src := PSmallInt(ptrs^[0]);
  System.Move(src^, DestA[0], nValues * SizeOf(SmallInt));

  src := PSmallInt(ptrs^[1]);
  System.Move(src^, DestB[0], nValues * SizeOf(SmallInt));

  // check ptrs^[2], ptrs^[3]... before using them
end;

Quick troubleshooting checklist

  • Map shortSmallInt (16 bits).
  • Multiply by SizeOf(SmallInt) when copying bytes.
  • Ensure calling convention matches (stdcall vs cdecl).
  • On 64‑bit builds, pointer sizes change — use Pointer/NativeUInt for arithmetic.
  • Validate pointers (not nil) and indexes before dereferencing to avoid AVs.

Also note ’s suggestion: use Move/System.Move for copying in Delphi; it’s the idiomatic replacement for memcpy.

Recommended Answers

All 6 Replies

MemCpy (Destination, Source, Num)
destination - Pointer to the destination array where the content is to be copied
source - Pointer to the source of data to be copied
num - Number of bytes to copy.

Delphi
procedure Move(const Source; var Dest; Count: Integer);

Move(Source, Destination, SizeOf(Destinaton)); { SizeOf = safety! }

I am sorry for not being clear. It is not the moving that I have a problem with but the de-referencing of **xBuffers to xBuffers[0] to xBuffers[3]

If I am right, **ptr is a pointer to an array of pointers to short integers... in delphi it is not used like this (thanks, Lord!).

If you know the lengths of the arrays when coding:

var a: array[0..10, 0..20] of short;

Then you pass a as paramter, and use a[0] as the first pointer, a[1]... etc.

If the lengths are not know (as i suppose), you just make this:

a: array of array of short;

And in the code, to set the lengths:

SetLength(a, 10); //10 pointers to arrays of shorts;
for i:= Low(a) to High(a) do
  SetLength(a[i], 20); //Each one with 20 elements (could be different for each array)

So you don't need to play with pointers of pointer to pointer that point to shorts or any other tipycal C puzzles, just pass an "a" and it is done.

The use of a: array of array worked a treat. Thank you.

int main()
{
string strtext("khiro delphi");
int iLength = strtext.length();
for (int i=0;i<=iLength;i++)
{ if (!(strtext[i]==' '))
        {
          cout<<strtext[i];
        }
  else 
cout<<endl; 
       }
return 0;
 }

Hi Khiro, you post a piece of code but don't ask anything at all... so?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.