rework malloc example
Graham Ollis authored 1 year, 6 months ago
Graham✈️✈️ committed 1 year, 6 months ago
1233 | 1233 |
|
1234 | 1234 |
```perl
|
1235 | 1235 |
use FFI::Platypus 2.00;
|
1236 | |
use FFI::Platypus::Memory qw( malloc free memcpy );
|
|
1236 |
use FFI::Platypus::Memory qw( malloc free memcpy strdup );
|
1237 | 1237 |
|
1238 | 1238 |
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;
|
1245 | 1247 |
free $buffer;
|
1246 | 1248 |
```
|
1247 | 1249 |
|
0 | 0 |
use strict;
|
1 | 1 |
use warnings;
|
2 | 2 |
use FFI::Platypus 2.00;
|
3 | |
use FFI::Platypus::Memory qw( malloc free memcpy );
|
|
3 |
use FFI::Platypus::Memory qw( malloc free memcpy strdup );
|
4 | 4 |
|
5 | 5 |
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");
|
7 | 8 |
|
8 | |
memcpy $buffer, $ffi->cast('string' => 'opaque', "hello there"), length "hello there\0";
|
|
9 |
memcpy $buffer, $ptr_string, 15;
|
9 | 10 |
|
10 | |
print $ffi->cast('opaque' => 'string', $buffer), "\n";
|
|
11 |
print $ffi->cast('opaque' => 'string', $buffer);
|
11 | 12 |
|
|
13 |
free $ptr_string;
|
12 | 14 |
free $buffer;
|