I am using an external C library (libsvm) from within C++. I insert the header file in my class header file using
extern "C"{
#include "svm.h"
}
This library contains a struct called svm_model. It also contains a function that given some input parameters it allocates (malloc) space for a struct svm_model and returns a pointer to it. The function is
svm_model *svm_train(input_parameters)
In my code (in C++) I create a variable in my class that is a struct svm_model pointer. In my header file I do
class myClass
{
public:
int do_something();
private:
struct svm_model *m_data;
}
Inside "do_something()" I have successfully called svm_train in the following way:
struct svm_model *test = svm_train(input_parameters);
But whenever I want to write the result into m_data, I get a segmentation_fault. This happens for
m_data = svm_train(input_parameters);
but also happens if I do
struct svm_model *test = svm_train(input_parameters);
m_data = test;
In fact, I noticed that even if I do
printf("hello: %p\n", m_data);
It also crashes. Therefore I suspect that there has to be a problem with using a pointer to a structure (which has been defined elsewhere) inside a class, although I have not found any hints anywhere. I tried initializing it to NULL in my class constructor, but does not change anything.
Any help is appreciated.