Created
January 17, 2018 16:34
-
-
Save mintisan/48ed9bc6afc9ebc4d7cb7e91f7f8f5fc to your computer and use it in GitHub Desktop.
ctype for array of pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
// mac : gcc -dynamiclib -o ctype_test.dylib ctype_test.c | |
/* | |
two dimensional pointer | |
*/ | |
void ctype_test(void *pa, int num, int size) | |
{ | |
printf(" %p\n", pa); | |
int **tmp = (int **)pa; | |
for (int i = 0; i < num; i ++ ) | |
{ | |
for (int j = 0; j < size; j++) | |
{ | |
tmp[i][j] = i * 10 + j; | |
} | |
} | |
} | |
/* | |
one dimensional pointer | |
*/ | |
int set(int *a, int size) | |
{ | |
printf("%p\n", a); | |
for (int i = 0; i < size; i++) | |
{ | |
a[i] = i; | |
} | |
} | |
int add(int a, int b) | |
{ | |
return (a + b); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import * | |
import numpy as np | |
################################### add ############################################ | |
dylib = CDLL('ctype_test.dylib') | |
print(dylib.add(12,14)) | |
################################### pointer ############################################ | |
d = np.zeros(10,dtype=np.uint32) | |
print(d) | |
dylib.set(d.ctypes.data_as(POINTER(c_int32)), len(d)) | |
print(d, d.ctypes.data_as(POINTER(c_int32)), hex(id(d.ctypes.data_as(POINTER(c_int32))))) | |
################################### array of pointers ############################################ | |
num = 6 | |
size = 7 | |
data = [] | |
for i in range(num): | |
data.append(np.zeros(size,dtype=np.uint32)) | |
arpDestBuffers = (POINTER(c_int32) * num)() | |
print(arpDestBuffers, hex(id(arpDestBuffers)), sizeof(arpDestBuffers)) | |
arpDestBuffers[:] = [d.ctypes.data_as(POINTER(c_int32)) for d in data] | |
dylib.ctype_test(arpDestBuffers, num, size) | |
for d in data: | |
print(d) | |
############################################################################### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
➜ Desktop python ctype_test.py | |
26 | |
[0 0 0 0 0 0 0 0 0 0] | |
0x7fe052600910 | |
[0 1 2 3 4 5 6 7 8 9] <__main__.LP_c_int object at 0x10897a6a8> 0x107c4f510 | |
<__main__.LP_c_int_Array_6 object at 0x10897a6a8> 0x10897a6a8 48 | |
0x7fe0548142d0 | |
[0 1 2 3 4 5 6] | |
[10 11 12 13 14 15 16] | |
[20 21 22 23 24 25 26] | |
[30 31 32 33 34 35 36] | |
[40 41 42 43 44 45 46] | |
[50 51 52 53 54 55 56] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment