简体   繁体   English

使用指向类内部结构的指针

[英]using a pointer to a struct inside a class

I am using an external C library (libsvm) from within C++. 我正在从C ++使用外部C库(libsvm)。 I insert the header file in my class header file using 我将头文件插入到我的类头文件中

extern "C"{
#include "svm.h"
}

This library contains a struct called svm_model. 该库包含一个名为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. 它还包含一个函数,该函数给定一些输入参数,该函数为svm_model结构分配(malloc)空间并返回指向它的指针。 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. 在我的代码中(在C ++中),我在类中创建了一个结构svm_model指针的变量。 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: 在“ do_something()”内部,我已通过以下方式成功调用了svm_train:

struct svm_model *test = svm_train(input_parameters);

But whenever I want to write the result into m_data, I get a segmentation_fault. 但是,每当我要将结果写入m_data时,都会遇到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. 我尝试在类构造函数中将其初始化为NULL,但未进行任何更改。

Any help is appreciated. 任何帮助表示赞赏。

If it crashes with simply 如果只是崩溃

  printf ("hello: %p\n", (void*)m_data);

then probably the issue is elsewhere and before. 那么问题可能出在其他地方和以前。 It looks like when you call that printf function this is invalid (perhaps NULL ?) or your memory heap is in very bad shape. 看来,当你调用像printf功能, this是无效的(也许是NULL ?),或者你的内存堆是在非常恶劣的形状。

On Linux, I would suggest to compile with g++ -Wall -g with a recent compiler (GCC 4.8 has just been released). 在Linux上,我建议使用最新的编译器(刚刚发布的GCC 4.8)使用g++ -Wall -g进行编译。 Improve the code till no warnings are given. 改进代码,直到没有警告。 Then use gdb and valgrind to debug it more. 然后使用gdbvalgrind对其进行更多调试。

You might want to also compile your libsvm with debugging information and all warnings (or simply, use the debug variant of that package). 您可能还希望使用调试信息和所有警告来编译libsvm (或者简单地使用该软件包的debug变体)。

The file svm.h already has the 文件svm.h已经具有

extern "C" {

declaration. 宣言。 So instead of: 所以代替:

extern "C"{
#include "svm.h"
}

simply do: 只需做:

#include "svm.h"

Also there is no need to repeat the struct key word again and again. 同样,也不需要一次又一次地重复struct关键字。 So instead of: 所以代替:

struct svm_model *m_data;

do: 做:

svm_model *m_data;

Well, just found the problem and it was indeed elsewhere than reported. 好吧,只是发现了问题,确实在报告之外的地方。 What happened is that I had created an STL vector of myClass and was accessing the methods on the first element of such vector, even though I had not explicitly created it, ie: 发生的事情是,我创建了myClass的STL向量,并且正在访问该向量的第一个元素上的方法,即使我没有显式创建它,即:

std::vector<myClass> dummy;
dummy[0].do_something();

Given that everything was compiling properly I did not think that this part of the code could give a problem. 鉴于一切都已正确编译,所以我认为代码的这一部分不会带来问题。

Thanks all for your help. 感谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM