[SciPy-user] swig c++ class storing pointer to array

T J tjhnson@gmail....
Thu Oct 30 19:43:45 CDT 2008


Hi,

I'm new to using swig and am struggling with arrays and python.  I
have created a C++ class which stores a pointer to an array as a
member.  Using the numpy.i interface file, I would like to be able to
pass in arrays from Python (without copying) to the constructor.  The
end goal is to be able to pass in an numpy array, use the C++ class to
do some operations on the array, create some new arrays, and then be
able to access those arrays (as they are updated) from within Python.

To get started, I've created a very basic file with implements the
storing of the arrays, and here is how I used it:

>>> import testme
>>> x = testme.MyClass([1,2,3])
>>> x.printme()
1.5865e-268
1.08557e-269
1.08557e-269
>>> a = numpy.array([1,2,3.])
>>> x = testme.MyClass(a)
>>> x.printme()
4.18205e-62
3.60756e-313
1.60847e-268

So I am getting uninitialized values.  When I look at
_wrap_new_MyClass in the wrapper file created by swig, I can insert:

  result->printme()

before "return resultobj", and the proper values are printed.  So I'm
not understanding why this isn't working...or what pointer is being
stored in the class.  I've attached all the necessary files.

Curiously, the following does seem to work:

>>> x = numpy.array([.5,.3,2.])
>>> y = numpy.array(x)
>>> z = testme.MyClass(y)
>>> z.printme()
0.5
0.3
2

Can someone explain this?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testme.i
Type: application/octet-stream
Size: 223 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/scipy-user/attachments/20081030/74faabb7/attachment-0002.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: numpy.i
Type: application/octet-stream
Size: 56153 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/scipy-user/attachments/20081030/74faabb7/attachment-0003.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testme.h
Type: text/x-chdr
Size: 162 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/scipy-user/attachments/20081030/74faabb7/attachment-0002.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testme.cpp
Type: text/x-c++src
Size: 248 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/scipy-user/attachments/20081030/74faabb7/attachment-0003.bin 


More information about the SciPy-user mailing list