[php-src] Issue #7949: FFI struct with flexible array member
| From: | cmb69 | Date: | Tue, 18 Jan 2022 15:54:59 +0000 |
| Subject: | [php-src] Issue #7949: FFI struct with flexible array member | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239104@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7949
Comment Author: cmb69
The primary purpose of ext/ffi is to interface with C libraries; so if your library exposes structs
with flexible array members and functions to create them, you should use these. You can't use
FFI::new() to allocate a struct with a flexible array member, but you can use it to
allocate a struct with a regular array member, and cast afterwards, e.g.
````php
$value = FFI::new('struct {
int32_t length;
char data[10];
}');
foreach (range('a', 'j') as $i => $char) {
$value->data[$i] = $char;
}
$alias = FFI::cast('struct {int32_t length; char data[];}', $value);
for ($i = 0; $i < 10; ++$i) {
echo $alias->data[$i];
}
````