How to use string variable in C extensions python

Viewed 46

I want to use string as member variable in python extensions C.

class Reader {
  unordered_map<string, long long> table;
  string val;
  public:
    void init(string name, long long num, string v);
}

void Reader::init(string name, long long num, string v) {
 this->table = unordered_map<string, long long>();
 this->table[name] = num;
 this->val = v;
}

In above code, when I assigned v in the string, error occurred.

Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x00007ffff5906c82 in std::string::_Rep::_M_is_leaked (this=0xffffffffffffffe8) at /usr/include/c++/9/bits/basic_string.h:3366
3366          _M_rep() const _GLIBCXX_NOEXCEPT

When I first write the code, I did not also initialize unordered_map variable table. Then error occurred. So I added initialization code. like this:

this->table = unordered_map<string, long long>();

When I built only that file with makefile, it never need initialized unorered_map too. But when I used the code in python extensions C. there was problem.

Unordered_map is resolved with adding initialized code. But error still occurred in windows. But string value is never initialized. I try to like this for initialization.

this->val = string(v); -> same SIGSEGV error
this->val(v); -> build error
this->val.assign(v); -> assign function error
Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x00007ffff5904d85 in __gnu_cxx::__exchange_and_add (__val=-1, __mem=0xfffffffffffffff8) at /usr/include/c++/9/ext/atomicity.h:82
82            return __exchange_and_add(__mem, __val);
(gdb) up
#1  __gnu_cxx::__exchange_and_add_dispatch (__val=-1, __mem=0xfffffffffffffff8) at /usr/include/c++/9/ext/atomicity.h:82
82            return __exchange_and_add(__mem, __val);
(gdb) up
#2  std::string::_Rep::_M_dispose (__a=..., this=0xffffffffffffffe8) at /usr/include/c++/9/bits/basic_string.h:3311
3311                  if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
(gdb) up
#3  std::string::_Rep::_M_dispose (__a=<synthetic pointer>..., this=0xffffffffffffffe8) at /usr/include/c++/9/bits/basic_string.h:3295
3295            _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
(gdb) up
#4  std::string::assign (__str=..., this=0x7ffff24d5ee8) at /usr/include/c++/9/bits/basic_string.tcc:700
700               _M_rep()->_M_dispose(__a);

How do I initialize class's member variable in python extensions C?

0 Answers
Related