由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 发现了一个编程思路,解决c++内存管理问题
相关主题
【C++】请问这样有没有memory leak?多谢请教c++数组初始化
JAVA 考试题请教急问大牛们:关于fortran堆栈溢出
Remove elements from multiple vectors in C++[合集] 一道M$面试题的解法... (转载)
能帮我看看Ruby的这道题吗?关于thread的stack
请教一个作用域的问题也问个STL的问题, 谢谢
[合集] 请问关于堆栈的问题Which is next book after "Effective C++"?
求教:c++中如何从raw data中创建对象?问个关于数组的问题
这个C++程序为什么不能运行c++里的函数可不可以是virtual+static
相关话题的讨论汇总
话题: testclass话题: ptr话题: share话题: shared
进入Programming版参与讨论
1 (共1页)
N******K
发帖数: 10202
1
比如一个类 TestClass
class TestClass
{
int m_value;
std::vector m_DataArray;
// memeber function
}
可以改为:
struct TestClassMemberVarible
{
int m_value;
std::vector m_DataArray;
}
class TestClass
{
std::shared_ptr m_MemberVarible;
// memeber function
void Share(TestClass& InputClass)
}
如果所有的类都这么设计 可以不用写 share_ptr 和 new 直接用堆栈
真正的类成员都在heap 所谓的类只是一个壳子+shared_ptr+函数 这样永远不会形成
shared_ptr 循环引用
也可以建立 一个类的array 类似 存在array的referece
TestClass A;
TestClass B;
std::vector SharedClassArray(2);
SharedClassArray[0].Share(A);
SharedClassArray[1].Share(B);
w******p
发帖数: 166
2
see Herb's way:
http:channel9.msdn.com/Events/GoingNative/2013/My-Favorite-Cpp-10-Liner
and see that over use of shared_ptr deteriorates to equivalence of mark-and-
sweep GC:
http:www.cs.virginia.edu/~cs415/reading/bacon-garbage.pdf
also shared_ptr new has mutex to protect [pointer+counter] operations so it'
s slower
k**********g
发帖数: 989
3
http://c2.com/cgi/wiki?HandleBodyPattern

【在 N******K 的大作中提到】
: 比如一个类 TestClass
: class TestClass
: {
: int m_value;
: std::vector m_DataArray;
: // memeber function
: }
: 可以改为:
: struct TestClassMemberVarible
: {

g*********e
发帖数: 14401
4
c++本来就是让你手动管理内存的,这个搞还不如写java
N******K
发帖数: 10202
5
具体说说?

【在 g*********e 的大作中提到】
: c++本来就是让你手动管理内存的,这个搞还不如写java
N******K
发帖数: 10202
6
具体说说?

【在 k**********g 的大作中提到】
: http://c2.com/cgi/wiki?HandleBodyPattern
N******K
发帖数: 10202
7
具体说说?

and-
it'

【在 w******p 的大作中提到】
: see Herb's way:
: http:channel9.msdn.com/Events/GoingNative/2013/My-Favorite-Cpp-10-Liner
: and see that over use of shared_ptr deteriorates to equivalence of mark-and-
: sweep GC:
: http:www.cs.virginia.edu/~cs415/reading/bacon-garbage.pdf
: also shared_ptr new has mutex to protect [pointer+counter] operations so it'
: s slower

k**********g
发帖数: 989
8

Basically it is doing what auto_ptr, unique_ptr and shared_ptr is doing
today.
However, the Handle-Body Idiom has a long history. Each software framework
implement it their own way, and combine it with framework-specific features
not found on the STL / C++11 smart pointers.
The user can only manipulate handles, and the handle doesn't directly own
the resource. Instead, the handle owns a reference-counted object (
implemented with an atomic variable) which then owns the actual resource.
When the user performs an assignment, as in the C++ assignment operator = ,
one of the three can happen:
(1) Copy = Share
(2) Copy = Move (taken away from the original handle and transferred to the
new handle)
(3) Copy = Copy-on-Write, activated by a pair of explicit calls to "
BeginWrite" and "EndWrite" (which would be used by a convenience RAII helper
to ensure timely release.)

【在 N******K 的大作中提到】
: 具体说说?
:
: and-
: it'

N******K
发帖数: 10202
9
你觉得我这么搞 有什么问题么?
我觉得挺好的 类全都变成了壳子->handle

features
,

【在 k**********g 的大作中提到】
:
: Basically it is doing what auto_ptr, unique_ptr and shared_ptr is doing
: today.
: However, the Handle-Body Idiom has a long history. Each software framework
: implement it their own way, and combine it with framework-specific features
: not found on the STL / C++11 smart pointers.
: The user can only manipulate handles, and the handle doesn't directly own
: the resource. Instead, the handle owns a reference-counted object (
: implemented with an atomic variable) which then owns the actual resource.
: When the user performs an assignment, as in the C++ assignment operator = ,

N******K
发帖数: 10202
10
为啥要手动管理? shared_ptr 吃干饭的?
c++有栈概念 或者说{} 非常好 加上 shared_ptr 可以随时干掉不用的对象

【在 g*********e 的大作中提到】
: c++本来就是让你手动管理内存的,这个搞还不如写java
k**********g
发帖数: 989
11

没问题,很好很强大,我只是善意提醒一下这方法有名堂,别人已经用了很多年了。

【在 N******K 的大作中提到】
: 你觉得我这么搞 有什么问题么?
: 我觉得挺好的 类全都变成了壳子->handle
:
: features
: ,

1 (共1页)
进入Programming版参与讨论
相关主题
c++里的函数可不可以是virtual+static请教一个作用域的问题
请教c++ non-vitura interface[合集] 请问关于堆栈的问题
紧急求助—寻C++ tutor求教:c++中如何从raw data中创建对象?
heap&stack Linux vs. Windows这个C++程序为什么不能运行
【C++】请问这样有没有memory leak?多谢请教c++数组初始化
JAVA 考试题请教急问大牛们:关于fortran堆栈溢出
Remove elements from multiple vectors in C++[合集] 一道M$面试题的解法... (转载)
能帮我看看Ruby的这道题吗?关于thread的stack
相关话题的讨论汇总
话题: testclass话题: ptr话题: share话题: shared