values = ffi.new( "int[]", 10 )
pValue = ffi.addressof( pInt, 0 )
Python CFFI では、上記のコードはvalues
asの最初の要素へのポインターを作成します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番」などと覚えておくと不便です...