2
values = ffi.new( "int[]", 10 )
pValue = ffi.addressof( pInt, 0 )

Python CFFI では、上記のコードはvaluesasの最初の要素へのポインターを作成しますpValue

その後、 でそのコンテンツにアクセスできますがvalues[ 0 ]、これは実際には透過的ではなく、どのインデックスがどの値であるかを追跡するのに不便な場合があります。

C *-operator、関数などpValue、コンテンツを直接逆参照してアクセスするものはありますか?

他の言語では... :

// In C:
// =====

int values[ 10 ] = {0};
int* pValue = &( values[ 0 ] );

func_with_pointer_to_int_as_param( pValue );

printf( "%d\n", *pValue );

-------------------------------------------------------------

# In Python with CFFI:
# ====================

values = ffi.new( "int[]", 10 )
pValue = ffi.addressof( values, 0 )

lib.func_with_pointer_to_int_as_param( pValue ) #lib is where the C functions are

print values[ 0 ] #Something else than that? Sort of "ffi.contentof( pValue )"?

編集:
これが役立つユースケースです:

私はそれがより読みやすいと思います:

pC_int = ffi.new( "int[]", 2 )
pType  = ffi.addressof( pC_int, 0 )
pValue = ffi.addressof( pC_int, 1 )
...

# That you access with:
print "Type: {0}, value: {1}".format( pC_int[ 0 ], pC_int[ 1 ] )

それよりも:

pInt_type = ffi.new( "int[]", 1 )
pType     = ffi.addressof( pInt_type, 0 )

pInt_value = ffi.new( "int[]", 1 )
pValue     = ffi.addressof( pInt_value, 0 )

...

# That you access with:
print "Type: {0}, value: {1}".format( pInt_type[ 0 ], pInt_value[ 0 ] )

そして、前者の方が速いと思います。しかし、値にアクセスしたい場合、「OKタイプは0番」などと覚えておくと不便です...

4

1 に答える 1

1

In C, the syntax *pType is always equivalent to pType[0]. So say you would like to do something like:

print "Type: {0}, value: {1}".format( *pType, *pValue )

but of course this is not valid Python syntax. The solution is that you can always rewrite it like this, which becomes valid Python syntax:

print "Type: {0}, value: {1}".format( pType[0], pValue[0] )
于 2016-07-22T09:46:04.990 に答える