Ask Your Question
1

How can a vector within a 2D array be converted into dart/flutter in order to be used with dart-ffi?

asked 2021-12-27 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-09-09 06:00:00 +0000

nofretete gravatar image

To convert a vector within a 2D array into Dart/Flutter, you can use the following steps:

  1. Define the struct in Dart/Flutter to match the corresponding struct in C. For example, if the vector within the 2D array is of type int, the struct definition in Dart/Flutter would look like this:
import 'dart:ffi' as ffi;

class ExampleStruct extends ffi.Struct {
  @ffi.Array.multi([10, 10]) // 10x10 2D array
  late ffi.Array<ffi.Array<ffi.Int32>> vector;
}
  1. Use the ffi.Pointer class to obtain a pointer to the 2D array. This pointer can be passed to an FFI function in order to operate on the array.
// assuming the 2D array is named `myArray`:
var struct = ExampleStruct()
  ..vector = ffi.Array<ffi.Array<ffi.Int32>>.generate(
    10, // outer dimension
    (i) => ffi.Array<ffi.Int32>.generate(
        10, // inner dimension
        (j) => myArray[i][j],
    ),
);
var structPointer = struct.addressOf;
  1. Pass the structPointer to an FFI function to operate on the array.
// assuming an FFI function named `operateOnArray`:
operateOnArray(structPointer);
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-27 11:00:00 +0000

Seen: 10 times

Last updated: Sep 09 '21