c/c++语言开发共享将任意大小的整数从Prolog传递到C.

现在,我正在学习如何使用C代码连接SICStus Prolog。

我想在SICStus Prolog版本4中使用/使用/查看任意大小的整数的“汉明重量”的C实现。

在我看来,我需要C函数来测试术语类型(SP_is_integer)和C函数来访问Prolog术语(SP_get_integer,SP_get_integer_bytes)。

但是,我不确定如何以便携,健壮的方式使用SP_get_integer_bytes。 你能指点一些精心设计的C代码做到这一点吗?

    使用它像这样:

    SP_term_ref tr = ... some term ... int native = 0; // want portable, little endian size_t buf_size = 0; if (!SP_get_integer_bytes(tr, NULL, &buf_size, native) // if buf_size was updated, then there was not really an error && buf_size == 0) { // Something wrong (eg, not an integer) return ERROR; } // here buf_size > 0 void *buffer = SP_malloc(buf_size); if (buffer == NULL) { return ERROR; } if (!SP_get_integer_bytes(tr, buffer, &buf_size, native)) { // Something wrong. This would be surprising here error(); } // Here buffer contains buf_size bytes, in // twos-complement, with the least significant bytes at lowest index. // ... do something with buffer ... // finally clean up SP_free(buffer); 

      以上就是c/c++开发分享将任意大小的整数从Prolog传递到C.相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/549549.html

      (0)
      上一篇 2021年1月14日
      下一篇 2021年1月14日

      精彩推荐