SWIG python初始化一个指向NULL的指针 [英] SWIG python initialise a pointer to NULL

查看:120
本文介绍了SWIG python初始化一个指向NULL的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理SWIG模块时,是否可以从python端将ptr初始化为NULL?

Is it possible to initialise a ptr to NULL from the python side when dealing with SWIG module?

例如,假设我将结构track_t包装在swig模块m(_m.so)中,则可以从python创建指向该结构的指针,如下所示:

For example, say I have wrapped a struct track_t in a swig module m (_m.so), I can create a pointer to the struct from python as follows:

import m

track = m.track_t()

这只会在适当的包装函数中为我分配一个track_t.

this will just malloc a track_t for me in the appropriate wrapper function.

但是我希望能够实现以下目标:

I would however like to be able to achieve the following:

track_t *track = NULL;

但是在python中而不是C中,例如从python端初始化一个指向NULL的指针

But in python rather than C, e.g. initialise a pointer to NULL from the python side

我需要执行此操作,因为这是哈希表C实现的要求,我正在使用新的哈希表以NULL指针开头

I need to do this as it is a requirement of the hash table C implementation I am using that new hash tables start with a NULL pointer

我可以编写一个辅助函数,例如track_t* create_null_track(),该函数返回一个NULL指针,但是认为可能有更简单的方法吗?

I could write a helper function, say, track_t* create_null_track() that returns a NULL pointer but thought there may be a simpler way?

我可以确认helper函数可以正常工作,但是我希望有一种内置的方法,因为需要NULL指针似乎很常见

I can confirm that the helper function works but I would expect there to be a built in way of doing this as requiring a NULL pointer would seem a common requirement

helper函数非常简单:

helper function is as simple as:

track_t* create_null_track(void)
{
    return NULL;
}

不确定函数中指定的返回类型信息是否必要,因此可能是更通用的方法:

not sure that the return type information specified in the function is necessary so a more generic approach could be:

void* get_null(void)
{
    return NULL;
}

也许?

推荐答案

只需使用python中的None即可代表空值.

Just use None from python to represent a null value.

这篇关于SWIG python初始化一个指向NULL的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆