I ran the following scripts which is considered as same, but the output is completely different,
can anyone explain why?
I first imported the necessary modules:
from ctypes import *
import numpy as np
Code1:
AOVoltage = np.linspace(-1, 1, 2200)
AOVoltage = AOVoltage.ctypes.data_as(POINTER(c_double))
print AOVoltage.contents
c_double(1.821347161578237e-284)
Code2:
a = np.linspace(-1, 1, 2200)
AOVoltage = a.ctypes.data_as(POINTER(c_double))
print AOVoltage.contents
c_double(-1.0)
Code3:
AOVoltage = (np.linspace(-1, 1, 2200)).ctypes.data_as(POINTER(c_double))
print AOVoltage.contents
c_double(1.821347161578237e-284)
For this to work, you need to retain a reference to the original
numpyarray to prevent it from being garbage collected. This is why #2 works, and #1 and #3 don’t (their behaviour is undefined).This is explained in the documentation: