dart-sdk/samples/ffi/sample_ffi_structs.dart
Leaf Petersen 745f112ef6 Opt out some files from null safety.
Opt an initial batch of files under samples, samples-dev,
utils, and runtime/tools/dartfuzz out of null safety in preparation
for switching the flag on by default.

Change-Id: Icdfd52a5a969e678a7205903332f73fe3841c223
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166960
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Leaf Petersen <leafp@google.com>
2020-10-12 22:15:42 +00:00

68 lines
1.7 KiB
Dart

// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'coordinate.dart';
main() {
print('start main');
{
// Allocates each coordinate separately in c memory.
Coordinate c1 = Coordinate.allocate(10.0, 10.0, nullptr);
Coordinate c2 = Coordinate.allocate(20.0, 20.0, c1.addressOf);
Coordinate c3 = Coordinate.allocate(30.0, 30.0, c2.addressOf);
c1.next = c3.addressOf;
Coordinate currentCoordinate = c1;
for (var i in [0, 1, 2, 3, 4]) {
currentCoordinate = currentCoordinate.next.ref;
print("${currentCoordinate.x}; ${currentCoordinate.y}");
}
free(c1.addressOf);
free(c2.addressOf);
free(c3.addressOf);
}
{
// Allocates coordinates consecutively in c memory.
Pointer<Coordinate> c1 = allocate<Coordinate>(count: 3);
Pointer<Coordinate> c2 = c1.elementAt(1);
Pointer<Coordinate> c3 = c1.elementAt(2);
c1.ref.x = 10.0;
c1.ref.y = 10.0;
c1.ref.next = c3;
c2.ref.x = 20.0;
c2.ref.y = 20.0;
c2.ref.next = c1;
c3.ref.x = 30.0;
c3.ref.y = 30.0;
c3.ref.next = c2;
Coordinate currentCoordinate = c1.ref;
for (var i in [0, 1, 2, 3, 4]) {
currentCoordinate = currentCoordinate.next.ref;
print("${currentCoordinate.x}; ${currentCoordinate.y}");
}
free(c1);
}
{
Coordinate c = Coordinate.allocate(10, 10, nullptr);
print(c is Coordinate);
print(c is Pointer<Void>);
print(c is Pointer);
free(c.addressOf);
}
print("end main");
}