Codebase list libffi-platypus-perl / e29b25c
rework malloc example Graham Ollis authored 1 year, 6 months ago Graham✈️✈️ committed 1 year, 6 months ago
2 changed file(s) with 15 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
12331233
12341234 ```perl
12351235 use FFI::Platypus 2.00;
1236 use FFI::Platypus::Memory qw( malloc free memcpy );
1236 use FFI::Platypus::Memory qw( malloc free memcpy strdup );
12371237
12381238 my $ffi = FFI::Platypus->new( api => 2 );
1239 my $buffer = malloc 12;
1240
1241 memcpy $buffer, $ffi->cast('string' => 'opaque', "hello there"), length "hello there\0";
1242
1243 print $ffi->cast('opaque' => 'string', $buffer), "\n";
1244
1239 my $buffer = malloc 14;
1240 my $ptr_string = strdup("hello there!!\n");
1241
1242 memcpy $buffer, $ptr_string, 15;
1243
1244 print $ffi->cast('opaque' => 'string', $buffer);
1245
1246 free $ptr_string;
12451247 free $buffer;
12461248 ```
12471249
00 use strict;
11 use warnings;
22 use FFI::Platypus 2.00;
3 use FFI::Platypus::Memory qw( malloc free memcpy );
3 use FFI::Platypus::Memory qw( malloc free memcpy strdup );
44
55 my $ffi = FFI::Platypus->new( api => 2 );
6 my $buffer = malloc 12;
6 my $buffer = malloc 14;
7 my $ptr_string = strdup("hello there!!\n");
78
8 memcpy $buffer, $ffi->cast('string' => 'opaque', "hello there"), length "hello there\0";
9 memcpy $buffer, $ptr_string, 15;
910
10 print $ffi->cast('opaque' => 'string', $buffer), "\n";
11 print $ffi->cast('opaque' => 'string', $buffer);
1112
13 free $ptr_string;
1214 free $buffer;