简体   繁体   English

python和c ++之间的对象的内存地址不相同

[英]the memory address of object between python and c++ are not identical

I am trying to print the memory address of the same object in both c++ and python with pybind11, but I found the returned memory address from both are not identical. 我试图用pybind11在c ++和python中打印同一对象的内存地址,但是我发现从两者返回的内存地址不相同。

c++ side C ++方面

class Example {
  public:
    Example() {std::cout << "constuctor" << this << std::endl;}
    ~Example() {std::cout << "destructor " << this << std::endl;}
};

class ABC {
  public:
    static std::unique_ptr<Example> get_example() {
      // std::shared_ptr<Example> ptr = std::make_shared<Example>();
      std::unique_ptr<Example> ptr = std::make_unique<Example>();
      return ptr;
    }
};

void init_example(py::module & m) {
    py::class_<ABC>(m, "ABC")
    .def_static("get_example", &ABC::get_example);
}

python side 蟒蛇边

example = my_module.ABC.get_example()
print (example)

the output 输出

constuctor0x234cd80
<Example object at 0x7f5493c37928>
destructor 0x234cd80

the memory address from c++ is 0x234cd80, but python is 0x7f5493c37928 来自c ++的内存地址为0x234cd80,但是python为0x7f5493c37928

any idea? 任何想法?

I am not familiar with the Python aspect of this question, but focusing on the C++ portion, you are not printing out the correct information. 我对这个问题的Python方面并不熟悉,但是专注于C ++部分,您没有打印出正确的信息。

The std::unique_ptr has a different address than the Example instance that is created, thus the values will be different. std::unique_ptr的地址与所创建的Example实例的地址不同,因此值将不同。 If you want to print the address of the item that the unique_ptr is addressing, you need to call the get() function. 如果要打印unique_ptr要寻址的项目的地址,则需要调用get()函数。

Here is a full example showing the differences: 这是显示差异的完整示例:

#include <memory>
#include <iostream>

class Example {
  public:
    Example() {std::cout << "constuctor " << this << std::endl;}
    ~Example() {std::cout << "destructor " << this << std::endl;}
};

class ABC {
  public:
    static std::unique_ptr<Example> get_example() 
    {
      std::unique_ptr<Example> ptr = std::make_unique<Example>();
      return ptr;
    }
};

int main()
{
    std::unique_ptr<Example> p = ABC::get_example();
    std::cout << "The unique_ptr address is: " << &p << std::endl;
    std::cout << "The get() function returns: " << p.get() << std::endl;
}

Output: 输出:

constuctor 0x555a68bd7c20
The unique_ptr address is: 0x7ffd9fa6c120
The get() function returns: 0x555a68bd7c20
destructor 0x555a68bd7c20

So you need to adjust your Python code to print the return value of get() . 因此,您需要调整Python代码以显示get()的返回值。

pybind11 creates a Python object which has a reference to C++ object. pybind11创建一个Python对象,该对象具有对C ++对象的引用。 So addresses of Python pybind11 wrapper and C++ object are different. 因此,Python pybind11包装器和C ++对象的地址不同。

The address in default pybind11 str object representation is the address of python object, not of underlying C++ object or it's smart pointer. 缺省pybind11 str对象表示中的地址是python对象的地址,而不是基础C ++对象或其智能指针的地址。

If you need to know address of C++ object add a method to you binding code as @PaulMcKenzie suggested. 如果您需要了解C ++对象的地址,请按照@PaulMcKenzie的建议在绑定代码中添加一个方法。

C++: C ++:

namespace py = pybind11;

PYBIND11_MODULE(example_module, m){
  m.def("add", add);

  py::class_<Example>(m,"Foo")
      .def(py::init())
      .def("get_raw_address",[](Example& foo){ return reinterpret_cast<uint64_t>(&foo);});

}

Python: 蟒蛇:

example = Example()
print(example)
print("C++ address: %x" % example.get_raw_address())

Output: 输出:

constuctor 0x10eff20
<example_module.Foo object at 0x7f51c71d4298>
C++ address: 10eff20
destructor 0x10eff20

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

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