Return struct causes segfault
Nicholas Earl <[email protected]> Sat, 28 Sep 2013 15:53:51 -0700
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <CANjQ-UeBhbGsj3SMT9KC38xNv2NBosgwQvTwLr7jU9OE5=L8_Q@mail.gmail.com> |
I'm struggling with returning a struct value using ctypes. Mainly, it keeps
segfaulting when I try and get the values from the returned object. I have
my python script setup like this:
import ctypes
import numpy as np
from numpy.ctypeslib import ndpointer
class PhotoInfo(ctypes.Structure):
_fields_ = [
('times', ctypes.POINTER(ctypes.c_double)),
('fluxes', ctypes.POINTER(ctypes.c_double)),
]
def main():
lib = ctypes.cdll.LoadLibrary('cfile.so')
start = lib.start
start.restype = ctypes.POINTER(PhotoInfo)
start.argtypes = [
ctypes.c_int,
ndpointer(ctypes.c_double),
]
N = 3
mass = np.array([0.00020335520, 5.977884E-05, 9.320397E-08])
phin = start(N, mass)
print phin[0].times[0]
And in my c file:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
typedef struct {
double *times;
double *fluxes;
} PhotoInfo;
extern "C" {
PhotoInfo *start(int N, double *mass);
}
PhotoInfo *start(int N, double *mass) {
PhotoInfo phin;
vector<double> times;
vector<double> fluxes;
// Here, I read in a file and fill times and fluxes with doubles using
// times.push_back(t) and fluxes.push_back(f)\
phin.times = ×[0];
phin.fluxes = ×[0];
return &phin;
}
Any guidance on this would be greatly appreciated!
Nicholas Earl
Graduate Research Assistant
San Diego State University
------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60133471&iu=/4140/ostg.clktrk
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users