加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 21:51:15 所属栏目:大数据 来源:网络整理
导读:我试图让这个 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 $thi
我试图让这个 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;后者虽然很慢,但往往会提供一些有关内存错误的有用信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读