動的配列(ポインタ)

2018/05/06

ポインタ変数

test.pyx(test.pyd)

# -*- coding: utf-8 -*-

from libc.stdlib cimport malloc,free

cdef struct Data:
    float x
    float y
    float z

cdef int data_count
cdef Data *data = <Data *>malloc(1 * sizeof(Data))

def setNumberOfData(n):
    global data,data_count
    mem = <Data *>malloc(n * sizeof(Data))
    if not mem:
        raise MemoryError()
    data = mem
    data_count = n

def setData(data_no,x=0,y=0,z=0):
    data[data_no].x = x
    data[data_no].y = y
    data[data_no].z = z

def lookData():
    for i in range(data_count):
        print(data[i].x,data[i].y,data[i].z)


test_ui.py

# -*- coding: utf-8 -*-
import test
test.setNumberOfData(10)
test.setData(4,1,2,3)
test.setData(5,10,12,13)
test.lookData()

Cython 0.28.2 documentation Memory Allocation
http://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html

配列を自由自在に作る
https://9cguide.appspot.com/19-01.html#S3