内存堆转储C++

2024-10-16 17:19:00 发布

您现在位置:Python中文网/ 问答频道 /正文

现在,我已经把Python称为C++。使用ctype在两者之间进行连接。在运行时,我有一个关于核心转储的问题。在

我有一个图书馆叫做libfst.so公司" 这是我的密码。 NGramFST.h.公司

#include <iostream>
class NGramFST{
private:
    static NGramFST* m_Instace;

public:
    NGramFST(){
    }

    static NGramFST* getInstance() {
        if (m_Instace == NULL){
            m_Instace = new NGramFST();
        }
        return m_Instace;
    }

    double getProbabilityOfWord(std::string word, std::string context) {
        std::cout << "reloading..." << std::endl;
        return 1;
    }

};

在NGramFST.cpp在

^{pr2}$

这是我的python代码。在

from ctypes import cdll
lib = cdll.LoadLibrary('./libfst.so')

#-------------------------main code------------------------
class FST(object):
    def __init__(self):
        print 'Initializing'

    def getProbabilityOfWord(self, word, context):
        lib.FST_getProbability(word, context)

fst = FST()
print fst.getProbabilityOfWord(c_wchar_p('jack london'), c_wchar_p('my name is'))

这是错误的

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

我又复习了一遍,但我发现我的问题在哪里。在


Tags: stringreturnsocontext公司staticclassword
2条回答

当我更改下面的python代码时,它就起作用了

string1 = "my string 1"
string2 = "my string 2"

# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')

print fst.getProbabilityOfWord(b_string1, b_string2)

和c++代码更改参数类型如下所示

^{pr2}$

^ }不理解C++类型(它不被称为^ {CD2>})。{cd3>无法处理}。它不知道您的函数需要std::string个参数。在

{1}与你的CDA接口兼容},需要与CDA接口兼容。extern "C"是必要的,但还不够。这些函数实际上需要从C调用

更好的是,使用一个现代的C++/Python绑定库,如pybind11。在

相关问题 更多 >