boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 老年工程师转行学C++的更新的问题
相关主题
怎样在Python里调用Visual C++的函数?
关于C++中const的问题
perl or python?
用python glue C++ 有什么好的办法
现在Window下写GUI的话用什么库?
坑: Python + "C++" plus SWAG as ONE big language
关于传递函数指针
c++标准函数传递一问
数组问题
请教一个程序调用的内存问题 (转载)
相关话题的讨论汇总
话题: c++话题: python话题: swig话题: 指针话题: obj
进入Programming版参与讨论
1 (共1页)
b***i
发帖数: 3043
1
这次是关于SWIG的,这是一种模板语言,用来和python等协作。比如,把C++的dll变成
pyd,可以自动完成很多补充的工作。
现在有这样一个发现,在python里面
a = cppapi.return_obj() C++函数返回一个对象,这个对象是python控制内存的
b = cppapi.return_ptr() c++函数返回一个指针,我认为python只控制这个指针的值
,不控制内存
我的问题是,如果
c = cppapi.return_obj2() 这里的C++函数这样写的:
Obj return_obj2(){
return * return_ptr();
}
这个方法的话,python的变量C来控制内存?就是说,不用的时候python负责释放内存。
我这个工作是不是和潮流反其道而行啊?大家都javascript了,我还C++?
c********1
发帖数: 5269
2
你可返回一个大内存,便于观察,python是否release the memory

【在 b***i 的大作中提到】
: 这次是关于SWIG的,这是一种模板语言,用来和python等协作。比如,把C++的dll变成
: pyd,可以自动完成很多补充的工作。
: 现在有这样一个发现,在python里面
: a = cppapi.return_obj() C++函数返回一个对象,这个对象是python控制内存的
: b = cppapi.return_ptr() c++函数返回一个指针,我认为python只控制这个指针的值
: ,不控制内存
: 我的问题是,如果
: c = cppapi.return_obj2() 这里的C++函数这样写的:
: Obj return_obj2(){
: return * return_ptr();

j*****g
发帖数: 254
3
loop 100 times
t**********n
发帖数: 1718
4
刷题转码 工资翻倍
m********5
发帖数: 17667
5
What you said is correct in default cases. The return_obj2 actually returned
a value not reference. So for sure for this copy of Obj the memory is
handled by python as default if you did not change the thisown flag. However
, you still need to manage the C++ copy of object (the return_ptr pointed to
). According to my experience, the C++ copy can be optimized away during
compiling in some cases, but this is special cases and no guarantee. If you
want the python handle the C++ created instance, you need make sure by
yourself that there is no other piece of code refer to that instance after
passing to python and manually change the thisown flag.

【在 b***i 的大作中提到】
: 这次是关于SWIG的,这是一种模板语言,用来和python等协作。比如,把C++的dll变成
: pyd,可以自动完成很多补充的工作。
: 现在有这样一个发现,在python里面
: a = cppapi.return_obj() C++函数返回一个对象,这个对象是python控制内存的
: b = cppapi.return_ptr() c++函数返回一个指针,我认为python只控制这个指针的值
: ,不控制内存
: 我的问题是,如果
: c = cppapi.return_obj2() 这里的C++函数这样写的:
: Obj return_obj2(){
: return * return_ptr();

b***i
发帖数: 3043
6
很好。现在发现我们的Python代码很多次使用了C++返回的指针,然后传给一个C++函数
来构造智能指针。这样是不对的
for..
xx = dict.get_somethin(...)
reg = ...
reg.add(xx)
我提出的解决方案是,进行复制。幸好我们的类都有Copy这个函数。
for..
xx = dict.get_somethin(...)
reg = ...
reg.add(xx.Copy())
现在的问题是,在SWIG怎么做?
%newobject *::Copy;
对吗?怎么才能知道是不是对的?在cxx文件能看到有什么改变吗?还是在py文件里可
以看到?

returned
However
to
you

【在 m********5 的大作中提到】
: What you said is correct in default cases. The return_obj2 actually returned
: a value not reference. So for sure for this copy of Obj the memory is
: handled by python as default if you did not change the thisown flag. However
: , you still need to manage the C++ copy of object (the return_ptr pointed to
: ). According to my experience, the C++ copy can be optimized away during
: compiling in some cases, but this is special cases and no guarantee. If you
: want the python handle the C++ created instance, you need make sure by
: yourself that there is no other piece of code refer to that instance after
: passing to python and manually change the thisown flag.

i********s
发帖数: 6
7
如果是在C++内生成的对象的指针,需要回到C++代码中去处理。
可以把指针传给PY代码,但是PY把这个指针作为一个特殊类型的数据来传递。当传递回
给C++代码之后,C++代码需要最后回收这个指针。
我怎么觉得你第一段代码看起来是对的?(如果最后在reg相应的C++代码里把指针回收
)。

【在 b***i 的大作中提到】
: 很好。现在发现我们的Python代码很多次使用了C++返回的指针,然后传给一个C++函数
: 来构造智能指针。这样是不对的
: for..
: xx = dict.get_somethin(...)
: reg = ...
: reg.add(xx)
: 我提出的解决方案是,进行复制。幸好我们的类都有Copy这个函数。
: for..
: xx = dict.get_somethin(...)
: reg = ...

b***i
发帖数: 3043
8
问题在于swig负责衔接,如果返回指针,那么swig默认C++传出的指针不应由swig来销
毁,就像vector删掉元素并不销毁内存一样,所以Python的变量如果出范围是
不会调用swig来销毁这个空间的。
返回对象则默认Python应该负责销毁,所以swig会帮助进行销毁。指针也可以达到类似
的效果,要使用%newobject cppapi::ReturnObj();
那么swig就认为C++把所有权交给了python,
a=cppapi.return_obj()就获得了所有权(swig设的域thisown)
当变量a出范围后,Python就可以告诉swig,swig再调用析构函数。

【在 i********s 的大作中提到】
: 如果是在C++内生成的对象的指针,需要回到C++代码中去处理。
: 可以把指针传给PY代码,但是PY把这个指针作为一个特殊类型的数据来传递。当传递回
: 给C++代码之后,C++代码需要最后回收这个指针。
: 我怎么觉得你第一段代码看起来是对的?(如果最后在reg相应的C++代码里把指针回收
: )。

1 (共1页)
进入Programming版参与讨论
相关主题
请教一个程序调用的内存问题 (转载)
C++容器放入对象或指针
各个公司做compiler赚钱吗?
C++函数里什么时候传入指针的引用和传入指针是不一样的?
在子函数内开内存,返回主函数指针然后释放空间是不是很糟糕的(转载)
一道很奇怪的面试题
琢磨了一下c++ smart pointer,发现不能到处用
C++ most vexing parse到底怎么回事?
请教:函数后面的 throw() 有意义么?
C里面一个被分配了内存的指针如何知道分配了多少?
相关话题的讨论汇总
话题: c++话题: python话题: swig话题: 指针话题: obj