首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >本机localtime()分段故障

本机localtime()分段故障
EN

Stack Overflow用户
提问于 2018-04-29 14:55:59
回答 1查看 173关注 0票数 9

在试图在Perl 6中公开localtime功能时,我似乎做错了什么:

代码语言:javascript
复制
use NativeCall;
my class TimeStruct is repr<CStruct> {
    has int32 $!tm_sec;
    has int32 $!tm_min;
    has int32 $!tm_hour;
    has int32 $!tm_mday;
    has int32 $!tm_mon;
    has int32 $!tm_year;
    has int32 $!tm_wday;
    has int32 $!tm_yday;
    has int32 $!tm_isdst;
    has Str   $!tm_zone;
    has long  $!tm_gmtoff;
}

sub localtime(uint32 $epoch --> TimeStruct) is native {*}
dd localtime(time);  # segfault

perl6-lldb-m下运行,我得到:

代码语言:javascript
复制
Process 82758 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x5ae5dda1)
    frame #0: 0x00007fffe852efb4 libsystem_c.dylib`_st_localsub + 13
libsystem_c.dylib`_st_localsub:
->  0x7fffe852efb4 <+13>: movq   (%rdi), %rax
    0x7fffe852efb7 <+16>: movq   %rax, -0x20(%rbp)
    0x7fffe852efbb <+20>: movq   0x8e71d3e(%rip), %rbx     ; lclptr
    0x7fffe852efc2 <+27>: testq  %rbx, %rbx
Target 0: (moar) stopped.

我在这里做错什么了吗?

更新:最后工作解决方案:

代码语言:javascript
复制
class TimeStruct is repr<CStruct> {
    has int32 $.tm_sec;    # *must* be public attributes
    has int32 $.tm_min;
    has int32 $.tm_hour;
    has int32 $.tm_mday;
    has int32 $.tm_mon;
    has int32 $.tm_year;
    has int32 $.tm_wday;
    has int32 $.tm_yday;
    has int32 $.tm_isdst;
    has long  $.tm_gmtoff; # these two were
    has Str   $.time_zone; # in the wrong order
}

sub localtime(int64 $epoch is rw --> TimeStruct) is native {*}

my int64 $epoch = time;  # needs a separate definition somehow
dd localtime($epoch);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-29 15:23:09

localtime()需要一个类型为time_t*的指针作为参数。假设time_tuint32_t在特定平台上是兼容的类型,

代码语言:javascript
复制
sub localtime(uint32 $epoch is rw --> TimeStruct) is native {*}
my uint32 $t = time;
dd localtime($t);

应该这样做(除非公开属性,否则什么也看不见)。

我有点惊讶于您的time_t不是64位类型,而且我刚刚搜索了apple time.h,我还怀疑您的struct声明中的最后两个属性的顺序是错误的.

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50087913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档