尝试使用 perl 数据结构(部分作为在 C 中支持关联数组的一种方式),但以下程序不断崩溃。它编译得很好,但会在使用 newSVpv() 时崩溃。不知道为什么。
#include <EXTERN.h> /* from the Perl distribution */
#include <perl.h> /* from the Perl distribution */
static PerlInterpreter *my_perl; /*** The Perl interpreter ***/
int main(int argc, char **argv, char **env) {
char *proto = "http";
SV* ret = newSVpv("http", 4);
sv_catpvn(ret, "://", 3);
STRLEN len = 1;
char *result = SvPV(ret, len);
printf("result: %p %d\n", result, len);
return 0;
}
编译它的行是
gcc -g -o interp te1.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
我拥有的 perl 是在 Ubuntu 12.04.4 LTS 上运行的 5.14.2。
感谢您的任何提示!
更新:添加了 gdb 跟踪。谢谢你的提问。gdb 跟踪:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b235a6 in Perl_newSVpv () from /usr/lib/libperl.so.5.14
(gdb) bt
#0 0x00007ffff7b235a6 in Perl_newSVpv () from /usr/lib/libperl.so.5.14
#1 0x0000000000400927 in main (argc=1, argv=0x7fffffffe1e8,
env=0x7fffffffe1f8) at te1.c:7
(gdb)