posix – 如何在NativeCall接口中正确使用CPointer和CStruct

我试图让这个 NativeCall运行的例子:
use NativeCall;

class p_timespec is repr('CPointer') {
    has uint32 $.tv_sec;
    has long $.tv_nanosecs;
}

sub clock_gettime(uint32 $clock-id,p_timespec $tspec --> uint32) is native(Str) { * };

my p_timespec $this-time;

my $result = clock_gettime( 0,$this-time);

say "$result,$this-time";

它只是段错误,这是你使用指针时发生的事情,你不应该.在这种情况下,可能是由于p_timespec的声明;我实际上已经宣布它为CPointer,虽然是the struct should be OK.但是,从分段错误我无法理解什么是真的错.有人可以帮忙吗?

解决方法

这里有两件事是错的.

>应使用CStruct表示
>您需要为其填充数据的结构实例,否则您将传递空指针

这似乎有效:

use NativeCall;

class p_timespec is repr('CStruct') {
    has uint32 $.tv_sec;
    has long $.tv_nanosecs;
}

sub clock_gettime(uint32 $clock-id,p_timespec $tspec --> uint32) is native(Str) { * };

my p_timespec $this-time .= new;

my $result = clock_gettime( 0,$this-time.tv_sec(),$this-time.tv_nanosecs()";

至于调试,Rakudo的安装过程还安装了perl6-gdb-m和perl6-valgrind-m;后者虽然很慢,但往往会提供一些有关内存错误的有用信息.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...