4

I am using dart:ffi.

I have a C function that accepts a pointer to array:

void myCFunc(const void *pointer);

I want to call it from dart, right now, I am calling it like this:

void myDartFunc(Uint8List data) {
  final Pointer<UnsignedChar> dataPtr = malloc(
    sizeOf<UnsignedChar>() * (data.length),
  );
  for (int i = 0; i < data.length; i++) {
    dataPtr[i] = data[i];
  }
  callMyCFunc(dataPtr);
}

Which is not so optimal for long lists as it copies the array into a new array.

Is there a way to get pointer to data without copying each element?

julemand101
  • 28,470
  • 5
  • 52
  • 48
HII
  • 3,420
  • 1
  • 14
  • 35
  • Maybe: [How to convert Uint8List to C equivalent datatype using dart:ffi](https://stackoverflow.com/q/62102889/5382650) – Craig Estey Nov 22 '22 at 01:59
  • @CraigEstey I think that answer suggests allocating memory then copying the array into it, which is what I want to avoid – HII Nov 22 '22 at 09:14
  • No, there's no way to avoid it. If you have a `Pointer` you can turn it into a typed list using `asTypedList` and then use `setAll` to (presumably) efficiently copy from/to another typed list. – Richard Heap Nov 24 '22 at 02:11

0 Answers0