how 2 write some code do automatic garbage collection?

Hmilu 2004-01-28 03:03:45
JavaPointer<MyOtherClass> p = new JavaPointer(new MyOtherClass());

keep p alive as long as there is someone referencing it. As
soon as it is no longer referenced, it should be deleted.
I am not sure when it will not referenced.
show me some hints,better give me codes.3ks
...全文
75 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wolf0403 2004-01-29
  • 打赏
  • 举报
回复
Should be the C++0x standard out soon... But where is it? And the responding compiler?
EricZhuo 2004-01-29
  • 打赏
  • 举报
回复
Bjarne 说在即将确立的C++0x新标准中将会给GC留下适当的接口,以便搭配不同的GC实作
Hmilu 2004-01-29
  • 打赏
  • 举报
回复
Go on!I wanna know the way to implement
something in C++ that acts in a way similart to a small piece of the GC.
Hmilu 2004-01-29
  • 打赏
  • 举报
回复
well.no matter,can change the name to C_Plus_Plus_Pointer.The name here is just nothing.
纪俊 2004-01-29
  • 打赏
  • 举报
回复
JavaPointer<MyOtherClass> p = new JavaPointer(new MyOtherClass());
I think U should move this tip to the Java forum.
pengzhenwanli 2004-01-29
  • 打赏
  • 举报
回复
换中文的。
在2003年的程序员合订本上,下册的开篇就是关于GC的问题。
各位前辈高人给了很好的解释。我不想重复贴出上面的问题。看过程序员的朋友应该知道。
给你下面的几个网址。
一个cir_ptr的源码
http://www.egroups.com/files/boost/circ_ptr.zip

pengzhenwanli 2004-01-29
  • 打赏
  • 举报
回复
Go on.
I think Bjarne is right.If you use C++,you should give up GC.
GC has many ways.
Reference count is a goog way,but like DaNiao say,it may lead deak lock.
If you read some articles,you should know.
for example, last year's PROGRAMMER has a series of articles and code.

fansses 2004-01-29
  • 打赏
  • 举报
回复
受不了也。我整中文的


爱母掐你日
sandrowjw 2004-01-29
  • 打赏
  • 举报
回复
Reference counting can accomplish your quest well, in spite of the underlying problem of cyclic reference.
Try memory pool to make memory management easy and nasty, although this requires some additional work.
As for the implementation, I think you ought to try before giving your further questions. You can refer to some COM examples(implemented in pure c++, without MFC or ATL) or boost code or loki code. That would not be too difficult for you, just try it!
Hmilu 2004-01-28
  • 打赏
  • 举报
回复
Good job,I will try and learn it more.

3ks pengzhenwanli(紫气日盈)
Wolf0403(完美废人,向数据结构致敬)
xiaocai365(小菜)


special thanks to DaNiao(鸿雁) :-)
xiaocai365 2004-01-28
  • 打赏
  • 举报
回复
Try autoptr. I think.
DaNiao 2004-01-28
  • 打赏
  • 举报
回复
In Java, GC works by scanning the stacks and registers of all the threads running in the JVM. If it finds something that looks like a reference into the Java heap, GC investigates further. If the reference actually is a reference to an object, then GC follows all the references in that object. The referenced object and all objects it references are "marked" (or flagged).
DaNiao 2004-01-28
  • 打赏
  • 举报
回复
Many people have attempted to implement the garbage collection feature in C++.
But all of the implementations are ineffective and hardly to be used.
The chief problem be write a garbage collector
(not only in C++ but also in Smalltalk, Eiffle, Java ... )
is how to determine whether an object have became a garbage and should be collected.
When a object is losing the only reference to it, U can collect it immediately.
But, if there is three object: A,B and C with the following relationship:
A refers to A
B refers to C
C refers to A
When will U collect them?
Typically U should have a gc thread to scan the whole heap and all other thread's stacks
to find such ABCs.
When the gc thread is running, all other thread should be blocked,
and if there are lots of objects in Ur system, the gc operation will take quite a long time.
This is why they don't support garbage collection in standard C++.

总算是写完了,越来越发现我英语真的是很烂。。。
Wolf0403 2004-01-28
  • 打赏
  • 举报
回复
Take a look at Boost Library. There's Shared_Ptr and some suits of other smart pointers. Some of them can manage resource using ref-count technic, which might fits your requirment.
GC is totally a different thing, i suppose
pengzhenwanli 2004-01-28
  • 打赏
  • 举报
回复
Too difficult!
If you like Java's GC,you can use Java.
If you want to C++ like Java has GC,its efficiency will be lower.
You can use autoptr .It can manage resource automatic.

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧