473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

cytpes **int

I am using ctypes to wrap a set of functions in a DLL. It has been
going very well, and I am very impressed with ctypes. I want to call a
c function with a signature of: void func(int **cube), where the array
if ints in cube is modified by func. I want to setup cube with int
values, and access them after the call to func. I unerstand how to
setup the ctypes array, but how do I pass **cube to the function, and
how do I access the results?
Jun 27 '08 #1
4 1055
VernM schrieb:
I am using ctypes to wrap a set of functions in a DLL. It has been
going very well, and I am very impressed with ctypes. I want to call a
c function with a signature of: void func(int **cube), where the array
if ints in cube is modified by func. I want to setup cube with int
values, and access them after the call to func. I unerstand how to
setup the ctypes array, but how do I pass **cube to the function, and
how do I access the results?
it should be simple.

use something like (untestet):

b = POINTER(c_int)()
func(byref(b))
Or

a = c_int()
b = pointer(a)
func(byref(b)

Or

b = POINTER(c_int)()
func(pointer(b))
Diez
Jun 27 '08 #2
En Mon, 28 Apr 2008 18:55:15 -0300, Diez B. Roggisch <de***@nospam.web.de>
escribió:
VernM schrieb:
>I am using ctypes to wrap a set of functions in a DLL. It has been
going very well, and I am very impressed with ctypes. I want to call a
c function with a signature of: void func(int **cube), where the array
if ints in cube is modified by func. I want to setup cube with int
values, and access them after the call to func. I unerstand how to
setup the ctypes array, but how do I pass **cube to the function, and
how do I access the results?

it should be simple.

use something like (untestet):

b = POINTER(c_int)()
func(byref(b))
[snip two other similar alternatives]

That's true for "a pointer to a pointer to int", and it's valid if the
functions references **b or b[0][0] - but in this case int** probably
means "[pointer to] an array of arrays of int" and presumibly the function
will try to access b[3][2] (or whatever indices are in range).
The duality pointer/array in C is dangerous when defining interfases - you
have to know how the value is intended to be accessed.
(I assume the function modifies the integer values, but not the pointers
themselves)

# build an array of 10x10 ints
Arr10int = c_int * 10
Pint = POINTER(c_int)
PPint = POINTER(Pint)
Arr10pint = Pint * 10
a = Arr10pint()
for i in range(10):
a[i] = Arr10int()
.... initialize the array ...
.... load the function ...
# call the function
somefunction.argtypes = (PPint,)
somefunction.restype = None
somefunction(a)

--
Gabriel Genellina

Jun 27 '08 #3
Gabriel Genellina schrieb:
En Mon, 28 Apr 2008 18:55:15 -0300, Diez B. Roggisch
<de***@nospam.web.deescribió:
>VernM schrieb:
>>I am using ctypes to wrap a set of functions in a DLL. It has been
going very well, and I am very impressed with ctypes. I want to call a
c function with a signature of: void func(int **cube), where the array
if ints in cube is modified by func. I want to setup cube with int
values, and access them after the call to func. I unerstand how to
setup the ctypes array, but how do I pass **cube to the function, and
how do I access the results?

it should be simple.

use something like (untestet):

b = POINTER(c_int)()
func(byref(b))

[snip two other similar alternatives]

That's true for "a pointer to a pointer to int", and it's valid if the
functions references **b or b[0][0] - but in this case int** probably
means "[pointer to] an array of arrays of int" and presumibly the
function will try to access b[3][2] (or whatever indices are in range).
The duality pointer/array in C is dangerous when defining interfases -
you have to know how the value is intended to be accessed.
(I assume the function modifies the integer values, but not the pointers
themselves)

# build an array of 10x10 ints
Arr10int = c_int * 10
Pint = POINTER(c_int)
PPint = POINTER(Pint)
Arr10pint = Pint * 10
a = Arr10pint()
for i in range(10):
a[i] = Arr10int()
... initialize the array ...
... load the function ...
# call the function
somefunction.argtypes = (PPint,)
somefunction.restype = None
somefunction(a)
Yup, you are right - I somehow missed the access description and thought
of the pointer-to-a-pointer as out-parameter-spec.

Diez
Jun 27 '08 #4
On Apr 28, 11:57*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Gabriel Genellina schrieb:


[snip repetition]
That's true for "a pointer to a pointer to int", and it's valid if the
functions references **b or b[0][0] - but in this case int** probably
means "[pointer to] an array of arrays of int" and presumibly the
function will try to access b[3][2] (or whatever indices are in range).
The duality pointer/array in C is dangerous when defining interfases -
you have to know how the value is intended to be accessed.
(I assume the function modifies the integer values, but not the pointers
themselves)
# build an array of 10x10 ints
Arr10int = c_int * 10
Pint = POINTER(c_int)
PPint = POINTER(Pint)
Arr10pint = Pint * 10
a = Arr10pint()
for i in range(10):
* * a[i] = Arr10int()
... initialize the array ...
... load the function ...
# call the function
somefunction.argtypes = (PPint,)
somefunction.restype = None
somefunction(a)

Yup, you are right - I somehow missed the access description and thought
of the pointer-to-a-pointer as out-parameter-spec.

Diez- Hide quoted text -

- Show quoted text -

To provide a little more detail: currently, a DLL function uses malloc
to create a pointer to a block of memory where the ints are stored
This pointer is returned to python. Then a pointer to this pointer is
passed to another C function which manipulates the ints. When that C
function returns, python needs to access the int values. I am now able
to get this to work with this grossly ugly code.

# Setup a place to store the *int pointer
pt = (ctypes.c_int * 1)
cube = pt()
cube[0] = dll.AllocCube() # Get the pointer to the ints

# Call the function that manipulates the ints
dll.FirstPrime(ctypes.byref(cube))

# Create a python list of the ints
result = [ctypes.c_int.from_address(cube[0]+i*4).value for i in
range(5)]

I appreciate the suggestions so far. I know there must be a cleaner
way to express this. I would prefer the array of ints to be built by
cytpes, rather than by a C function in the DLL.


Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bengt Richter | last post by:
Peculiar boundary cases: >>> 2.0**31-1.0 2147483647.0 >>> int(2147483647.0) 2147483647L >>> int(2147483647L ) 2147483647 >>> >>> -2.0**31
4
by: J. Campbell | last post by:
I'm a novice with c/c++ and have been reading Eckel's book. I'd like some feedback on using this method. What I need to do is treat a string as numeric data. I know how to write functions to...
2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
7
by: Ben | last post by:
Hi all, I'm not yet good at thinking the right way in c++ so although I could solve this problem, I'm not sure if they way I'm thinking of is the best way to do it. I need a data type or class...
1
by: akickdoe22 | last post by:
Please help me finish this program. i have completed the addition and the subtraction parts, but i am stuck on the multiplication and division. any suggestions, hints, code, anyhting. it's not a...
4
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
14
by: yang__lee | last post by:
Hi, You all know typedef typedef struct g { int a; int b; } google;
16
by: Julia | last post by:
Hi, there, In C programming, for pointer, I saw two programming styles: one is connecting '*' with variable, like, 'int *i'; the other is connecting '*' with data type, like, 'int* i' I...
8
by: beagle197 | last post by:
Folks, Attempting to q-sort an array of int pairs, e.g. {{0,1}, {0, 0}, ...} allocated using malloc/calloc, but the arguments I'm passing to qsort are producing the incorrect results (see...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.