I am trying to get array of 4 x int data using struct from c to dart via ffi.The code is compiling fine but the value i got in dart are garbage value that means i am goofing up in allocating memory.

And i am not sure which method to use for passing this value to dart side:

  1. using struct (as done right now)
  2. creating a normal 1d array and assigning the value in a pattern
  3. creating a vector but i dont think so there is a method to return a vector

my dart side code :

Uint8List imageContour(int width, int height, Uint8List imgBytes) {
    //passing Uint8List
    Pointer<Uint8> imgPtr = malloc.allocate<Uint8>(imgBytes.lengthInBytes);
    imgPtr.asTypedList(imgBytes.length).setAll(0, imgBytes);

    //creating Uint8List for grayscale
    Pointer<Uint8> encodedImgPtr = malloc.allocate<Uint8>(25 * 70);
    Pointer<countourPt> retContour = malloc.allocate<countourPt>(5);//struct allocation
    int contourLen =
        _imageCon(imgBytes.lengthInBytes, imgPtr, encodedImgPtr, retContour);

    Uint8List encodedImBytes = encodedImgPtr.asTypedList(25 * 70);
    print(retContour[0].h);//printing garbage value
    return encodedImBytes;

class countourPt extends Struct {
  external int x;
  external int y;
  external int h;
  external int w;

c side code :

const int
    imageContour(int inBytesCount, uchar *rawBytes, uchar *encodedOutput, struct countourPt *retContours)
        long long start = get_now();
        vector<uint8_t> buffer(rawBytes, rawBytes + inBytesCount);
        Mat input = imdecode(buffer, IMREAD_COLOR);

        vector<vector<Point>> contours;
        vector<Vec4i> hierarchy;

        cvtColor(input, input, COLOR_BGR2GRAY);
        threshold(input, input, 22, 255, THRESH_BINARY_INV);
        findContours(input, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
        struct countourPt *retContoursC = (struct countourPt *)malloc(sizeof(countourPt) * contours.size());//struct allocation
        for (int i = 0; i < contours.size(); i++)//assigning value in dynamic struct
            Rect pt = boundingRect(contours[i]);
            // cout << pt.x << " " << pt.y << " | " << pt.height << " " << pt.width << endl;
            retContoursC[i].x = pt.x;
            retContoursC[i].y = pt.y;
            retContoursC[i].h = pt.height;
            retContoursC[i].w = pt.width;
        platform_log("constour size %d\n", (contours.size()));
        memcpy(retContours, retContoursC, sizeof(sizeof(countourPt) * contours.size()));//copying stuct to dart side memory
        memcpy(encodedOutput, input.data, 25 * 70 * sizeof(uint8_t));
        int evalInMillis = static_cast<int>(get_now() - start);
        platform_log("Processing done in %dms\n", evalInMillis);
        return contours.size();