5

我想在我的 C 代码 (XS) 中使用 Perl 哈希作为一个集合,所以我只需要将键保存在哈希中。是否可以存储诸如 null 或其他常量值之类的内容以避免创建不必要的值?

像这样的东西:

int add_value(HV *hash, SV *value)
{
    // just an example of key
    char key[64];
    sprintf(key, "%p", value);
    if (hv_exists(hash, key, strlen(key)) return 0;

    // here I need something instead of ?
    return hv_stores(hash, key, ?) != NULL;
}

一种可能的解决方案可能是存储值本身,但也许有特殊的常量 forundef或 null。

4

2 回答 2

5

&PL_sv_undef是未定义的值,但不幸的是,您不能在散列和数组中天真地使用它。引用perlguts

通常,如果您想在 AV 或 HV 中存储未定义的值,则不应使用 &PL_sv_undef ,而应使用 newSV 函数创建新的未定义值,例如:

av_store( av, 42, newSV(0) );
hv_store( hv, "foo", 3, newSV(0), 0 );
于 2015-08-05T15:45:58.150 回答
4

&PL_sv_undef是undef 标量它是只读的。您可能需要一个新的 undef 标量,它是使用newSV(0)[1]创建的。

返回的标量newSV(0)以 1 的引用计数开始,当标量使用 存储在其中时,哈希“占有” hv_stores,所以不要SvREFCNT_decsv_2mortal返回的标量。(如果您也将其存储在其他地方,请增加引用计数。)


  1. # "The" undef (A specific read-only variable that will never get deallocated)
    $ perl -MDevel::Peek -e'Dump(undef)'
    SV = NULL(0x0) at 0x3596700
      REFCNT = 2147483641
      FLAGS = (READONLY,PROTECT)
    
    # "An" undef (It's not the type of SVt_NULL that make it undef...)
    $ perl -MDevel::Peek -e'Dump($x)'
    SV = NULL(0x0) at 0x1bb7880
      REFCNT = 1
      FLAGS = ()
    
    # Another undef (... It's the lack of "OK" flags that make it undef)
    $ perl -MDevel::Peek -e'$x="abc"; $x=undef; Dump($x)'
    SV = PV(0x3d5f360) at 0x3d86590
      REFCNT = 1
      FLAGS = ()
      PV = 0
    
于 2015-08-05T15:47:10.110 回答