由买买提看人间百态

topics

全部话题 - 话题: raii
1 2 3 4 下页 末页 (共4页)
m**********s
发帖数: 518
1
来自主题: Programming版 - RAII和GC对应的两条技术路线
恰恰相反,GC是更高瞻远瞩的技术路线,RAII是局部优化对付对付。
GC假定机器比人可靠,假定算法最终能自动释放资源。
RAII强迫码农采用特定的范式去编程,它假定人对范式的理解是一致的,并且人是不犯
错误的。
回到现实世界,GC并不最优因为算法不够牛;RAII也一样,再牛的码工也不可能不写出
bug,而且码工越资深,埋的bug越隐蔽越致命。

GC: immediate solution,表面上看似解决了问题,实际上只是部分解决了问题,而且
同时创造了不止一个新的问题。RAII: 本质的解决方案,不止解决了内存分配一类....
....
w***g
发帖数: 5958
2
来自主题: Programming版 - RAII和GC对应的两条技术路线
GC: immediate solution,表面上看似解决了问题,实际上只是部分
解决了问题,而且同时创造了不止一个新的问题。
RAII: 本质的解决方案,不止解决了内存分配一类问题(这个也是部分
解决),而且同时也解决了所有其他资源分配的问题。
从技术的角度上来说,我觉得这两者高下立判。看似都没有显式的
资源回收,前者是纵容用户,后者则是培养一种思维方式,写出来
的东西都是防患与未然的。就是代码写出来,也是C++的更简洁:
// C++
{
fstream out("xxx");
out << "foo" << endl;
}
//java
OutputStream out = new FileOutputStream("xxx");
out.write("foo\n");
out.close();
可以看到,C++写法里既没有new,也没有close。一对花括号限死了
fstream中所有相关资源的生存期。
而java里,从C++借鉴来一个完全没有必要的new,却没有对应的delete,
而后面的close又没有对应的open。代码没有一点对称性。现在看来,
用来分配内存... 阅读全帖
k******n
发帖数: 133
3
【 以下文字转载自 Pharmaceutical 讨论区 】
发信人: kevinwan (kevin), 信区: Pharmaceutical
标 题: RAI/ RAII position available at a Mid-size Biotech (DC area)
发信站: BBS 未名空间站 (Mon Nov 29 18:53:00 2010, 美东)
Previous experience with HPLC is required. Knowledge of protein Biochemistry
is highly desirable.
Please send over your cover letter to this mitbbs account.
Brief description:
Our company, a mid-size biotechnology company at Maryland has a new
opportunity for a research associate I/II position in the Physicochemi... 阅读全帖
a**a
发帖数: 416
4
来自主题: Programming版 - 用gc的语言是不是就不能用RAII了?
如果用python的话,提供两种方法:
一个是with statement. 比如
with file('foo.bat') as fd:
# do whatever with fd
在退出with statement的时候可以自动释放资源。当然这个需要对象支持某些方法。
另外一个是在删除对象的时候调用对象的__del__方法。
Python的gc的算法之一是引用计数。所以当一个对象退出作用域且引用计数为零时
将调用该对象的__del__方法。
总而言之,语言可以通过支持某种协议来实现RAII. 如果不行的话,只能通过框架
来实现了。Python的做法应该就是语言级别的框架协议。
n****1
发帖数: 1136
5
1. ref count其实只是GC strategy的一种, 而且是非常普遍的一种. 所以zhaoce拿ref
count来说事是很不妥的.
"Tracing and reference counting are uniformly viewed as being fundamentally
different approaches to garbage collection that possess very distinct
performance properties. [...] they are in fact duals of each other. [...]
all high-performance collectors [...] are in fact hybrids of tracing and
reference counting."
2. 内存管理策略有很多种,GC(包括ref count)只是最无脑的一种. 其他的策略往往是
根据具体模型的特点, 在适当的时候高效地申请和释放内存. 内存池就是典型的例子.
java直接rule out这种可能性.
不要老说... 阅读全帖
g*****g
发帖数: 34805
6
来自主题: Programming版 - RAII和GC对应的两条技术路线
RAII is a partial solution at best.
1. It's far from elegant once you allocate from heap and start using smart
pointer. And smart point has the fatal issues of not able to dealing with
circular pointers etc. And let's face it, not everything can be stored in
stack. i.e. It's a mumbo jumbo technique at best.
2. It's not enforced by compiler at all. You can't even count on your
colleague doing it consistently, let alone 3rd party.
At the end of the day, memory leak is still consistently an issue, ... 阅读全帖
d*******r
发帖数: 3299
7
来自主题: Programming版 - RAII和GC对应的两条技术路线
C++ 高手们展开说说, 如果有很多地方都 share 了同一块内存,
比如一个 shared Object in heap (难道说这个应用场景 C++ 高手不使用?)
内存回收逻辑放入 shared Object destructor 吧.
如何总是能简洁并保险地回收内存? 还是能用 RAII 风格来回收?
k***j
发帖数: 411
8
来自主题: Programming版 - RAII和GC对应的两条技术路线
一点屁大点的编译器特性,让你说的跟诈尸了似的。。。
楼主肯定没读过c++ standard doc
RAII是编程范式
GC是内存管理机制
完全两种不同概念,有什么好混在一起谈的
g*****g
发帖数: 34805
9
来自主题: Programming版 - RAII和GC对应的两条技术路线
It's an ad hoc practice at best and it's not easy to detect when a mistake
is made, that's far inferior to a GC solution.
If you think you can write GC better than dedicated GC deveoper, think again
. It's OK to use C++ when latency is critical, but if you believe RAII is a
better solution in general, you must be smoking.
y*j
发帖数: 3139
10
来自主题: Programming版 - RAII和GC对应的两条技术路线
java exception check helps。很多人写c++忘了这一点。
如果用RAII,非内存资源会自动释放的

the
C
w***g
发帖数: 5958
11
来自主题: Programming版 - RAII和GC对应的两条技术路线
RAII和GC是解决资源管理问题的两种不同的方法。一种通过编程范式解决,另一种通过
runtime/库解决。GC也可以用来解决别的资源,比如文件,锁等的自动释放问题。
混在一起谈很合适啊。
k**********g
发帖数: 989
12
来自主题: Programming版 - RAII和GC对应的两条技术路线
刚复习了一次Java的 Closeable, AutoCloseable 文档,蓦然回首,发现葵版感觉的很
穿越。。。
早就說了 RAII 的名字改得不好,應該是 Scope-based resource management 才對。
y*j
发帖数: 3139
13
来自主题: Programming版 - RAII和GC对应的两条技术路线
咦,你不正好说明Java也开始用RAII管理资源吗?
z*********n
发帖数: 1451
14
来自主题: Programming版 - c++ 怎么突然加了这么多feature

RAII绝对是个好的concept,比Java GC强多了。C#有GC也照样学C++实现RAII. GC最好也
是最烂的原因都是它不让你自己控制何时如何做GC。RAII天生杜绝内存(连接,etc)泄
露同时,还让程序员自己有控制权。用惯RAII,再用其他语言,那才叫个不适宜。关个
数据库连接try catch finally {try catch finally{..}} wtf?
g****t
发帖数: 31659
15
来自主题: Programming版 - c++ 怎么突然加了这么多feature
内存泄漏理论上等价于停机问题
现实中和软件潮流关系很大
也就是行业流行怎么写关系很大
一个好的群体习惯建立后
可能短期内在行业里
内存等约束不是问题了
但是因为竞争
软件天才们总会改写法走到旧写法的极致状态
然后会把这些约束的边界打穿来谋取
超额利润
再然后就会产生新的底层改进的需求
最后,内存问题,我个人一点浅见:
Rust 那个线性逻辑从理论上和实际上我觉得靠谱
C 应该还是c 11占主导位置


: RAII绝对是个好的concept,比Java GC强多了。C#有GC也照样学C 实现
RAII. GC
最好也

: 是最烂的原因都是它不让你自己控制何时如何做GC。RAII天生杜绝内存(
连接,
etc)泄

: 露同时,还让程序员自己有控制权。用惯RAII,再用其他语言,那才叫个
不适宜
。关个

: 数据库连接try catch finally {try catch finally{..}} wtf?

d**********x
发帖数: 4083
16
来自主题: Programming版 - 面试C/C++该怎么准备?
W用struct模拟的继承还是比较笨拙的...尤其是多继承,我没接触过太复杂的c代码,不
知道能不能比较好的模拟出来,感觉上应该比较难吧。
而且RAII机制也是相当大的问题。。只用c的话我不觉得有什么地方可以实现RAII。至于
类型安全,就更加糟糕了。。
当然这个只适合比较大型的系统,要是几万行代码的东西,c还是cpp,java都一个鸟样
。。
w***g
发帖数: 5958
17
我最近三五年编程序发现C++绝大多数和动态分配相关的情况都可以用RAII和const指针
来实现。其中RAII绝大部分又只要std::vector和std::map就够了。基本上杜绝了动态内
存分配的所有问题。不过指针的存在还有一个隐患,就是指针如果没有初始化或者指错
了的话可以摧毁内存中的数据结构。我自己觉得C++缺的一大块是boost::sql以及搭建
在其上的boost::orm。搞定这两个的话一大批应用就都解决了。
最近几个月我同时写python和C++,最让我受不了的是python没有编译期查错, 有时候
一个程序运行了三四个小时才运行到语法错误行,非常受不了(我习惯不好, 一次性
运行的程序不怎么写测试)。而且规则越松散程序就越容易出错, 有时候变量名敲错
了这种错误真的很难发现。
t****t
发帖数: 6806
18
smart pointer是为了RAII做的. 因为有exception的引入, 使得资源需要在多处释放,
而stack unwinding对传统指针没用, 所以有了所谓smart pointer. 当然最初的auto_
ptr那是一个四不像, 对copy和move很不友好, 所以引入了shared_ptr和unique_ptr,
以及moving semantics. 这和GC完全不是一回事, 目的不同, 手段也不同. 反过来, 因
为GC的存在, core java要做RAII就不是那么直接.

去查
j******t
发帖数: 788
19
No, I guess we are not talking about the same thing.
RAII is all about the allocation/deallocation of resource. It is just one of
usages of smart pointer, which actually was shared_pointer when RAII
mechanism was born. But that is not the reason people invented smart
pointer, I guess you are confused with the two idioms, smart pointer is much
much more than either shared_pointer or autopointer. Let's just forget
about these two dead words. Do not assume they are the same as smart pointer
.

,
g****u
发帖数: 252
20
来自主题: Programming版 - C++的smart pointer注定是个二流的东西
如果所有的指针都用smart pointer包装, 只能说这个语言设计得失败. 其实C++的王道
是RAII. 只要坚持RAII, 所有的资源都在同一对花括号内获取和释放,指针根本不会用
错, 用不着什么smart pointer. 在加上smart pointer名目和种类繁多,都快赶上茴字
的四种写法了, 连我这个写了10年C/C++的都不怎么搞得清(smart pointer这东西至少
10年前就应该有了), 一旦普及起来必将bug百出引入无穷后患.
l*s
发帖数: 11
21
来自主题: Programming版 - Python的With語句嚇著我了
沒寫過Python代碼,看別人的程序裡面有很多with open(file) as f這類的語句,一下
子把我嚇住了。Google了半天,原來就是C++裡面的RAII啊!Python裡面叫做Context
Managers。搞得這麼花裡胡哨的有什麼用呢?這樣的Context Managers的生存週期因為
Garbage Collection而不能確定,遠不如C++來得簡單直接精確,況且RAII是OOP的原生
特性,Python後來添加這個Context Managers純粹就是玩一些語法格式上的遊戲,無甚
用處。
p***o
发帖数: 1252
22
来自主题: Programming版 - java8的stream就是个半成品
1 C++用RAII,没有finally也不需要finally。
2 try-with-resource这类的就是有GC语言的RAII,Python/C#类似的结构早就加上了。
l*********s
发帖数: 5409
23
来自主题: Programming版 - 真是痛恨喜欢 throw 的猪
RAII solves resource management issues created by exceptions, but RAII has
other merits and exception has more issues. You cannot equate them.
New standard has deprecated exception declaration, if you consider that is
an endorsement of throwing exception, I don't know what to tell you.
n***y
发帖数: 2730
24
来自主题: Military版 - 据说C++是非常复杂的一个语言?
C++是要会用:
stl algorithm/container
boost
template
metaprogramming
raii
most importantly, use shared_ptr.
s*********i
发帖数: 916
25
2006年的infiniti Fx35 AWD 78000迈
KBB price
Excellent $22,800
Very Good $22,200
Good $21,650
http://www.kbb.com/infiniti/fx/2006-infiniti-fx/fx35-sport-util
我卖 $21,000 可议价 New Jersey& New York City Only
很美的宝蓝色 全皮座椅 电动加热 无限蓝牙 倒车摄像头
2009年10月我买这个车的时候,这个车一直是dealer在开,第一个owner是infiniti公
司。所以车特别新,除了驾驶员皮座椅能看出来有人用过,其他座椅都是全新的。然后
我开这两年,也十分仔细。无吸烟车,没有小孩子和小狗,干干净净,经常去infiniti
店里做保养,有单据可以提供。去年夏天在local交通灯左转,被一个福特车刮了驾驶
员那边的后门边,留了个小坑。对方保险公司只出50%赔偿,朋友们都说不必修,赚那
两千多刀。我心疼车,自己又添了些钱,把整个后门换了新的。但是再就没有别的事故和刮碰了。
去年冬天花了一千多美金换了4... 阅读全帖
l*********y
发帖数: 44
26
来自主题: JobHunting版 - 电面面经
三番一个太阳能公司。职位embedded se.
问了很多问题。大概持续了一个小时,没有coding.
除了问简历外:
0. 对linux的熟悉程度。
01. gdb
02. why endian metters in network programming
1. vector和list区别。
2. 在知道不会修改variable的情况下,为什么要用const
3. diamond problem and how to avoid
4. about STL & raii
5. tarity
6. exception handling
7. bit manipulation
i***1
发帖数: 147
27
来自主题: JobHunting版 - M$ screening coding题2道
因为没有什么保密协议就直接粘贴了...
如果有人认为不妥 麻烦联系
1. Implement a thread-safe circular queue of integers of user-specified size using a simple array. Provide
routines to initialize(), enqueue() and dequeue() the queue.
The solution should include a set of test functions that verify the queue is thread-safe.
2. Implement an RAII wrapper class for Win32 handles. The class should be
able to accept a handle from any Win32 function that returns a handle (CreateFile,
CreateThread, etc.). The class should allow copyin... 阅读全帖
j********x
发帖数: 2330
28
RAII
c**1
发帖数: 71
29
Yellowleaf's implementation looks correct to me. some minor issue though.
* in c++, it is better to use RAII to lock / unlock mutex
* no need to signal if --readerCount != 0
c*****e
发帖数: 737
30
来自主题: JobHunting版 - B 家 电话题 C++
RAII
s**x
发帖数: 7506
31
来自主题: JobHunting版 - 请教 C++ exception 面试问题
this is too basic.
memory leak of course. how to solve it? google RAII.
z*********n
发帖数: 1451
32
来自主题: JobHunting版 - 现在C++的码农还有公司要吗?

C++内存泄漏是问题?RAII比Java GC好用太多了吧。
l********k
发帖数: 393
33
来自主题: Parenting版 - 在家带小孩的负面问题
你妻子原来是学生物的。 你就帮她在你的学校里找个实验室作技术员, 有些实验室不
看文凭的, 我原来实验室老板就找了个高中毕业,没有一点儿生物背景的50多岁的老
美做RAI,现在几年过去了转成RAII了。 关键是熟人介绍。 一开始找不到技术员的工
作,可以在实验室做volunteer, 做的熟练了,很有可能给办h1的。
s*********i
发帖数: 916
34
【 以下文字转载自 Automobile 讨论区 】
发信人: shushanlili (shushanlili), 信区: Automobile
标 题: [卖] 2006 INFINITI FX35 AWD BLUE 78000+迈 $19,000
发信站: BBS 未名空间站 (Mon Jan 23 18:57:34 2012, 美东)
2006年的infiniti Fx35 AWD 78000迈
KBB price
Excellent $22,800
Very Good $22,200
Good $21,650
http://www.kbb.com/infiniti/fx/2006-infiniti-fx/fx35-sport-util
我卖 $21,000 可议价 New Jersey& New York City Only
很美的宝蓝色 全皮座椅 电动加热 无限蓝牙 倒车摄像头
2009年10月我买这个车的时候,这个车一直是dealer在开,第一个owner是infiniti公
司。所以车特别新,除了驾驶员皮座椅能看出来有人用过,其他座椅都是全新的。然后
我开这两年,也十... 阅读全帖
s*********i
发帖数: 916
35
2006年的infiniti Fx35 AWD 78000迈
KBB price
http://www.kbb.com/infiniti/fx/2006-infiniti-fx/fx35-sport-util
我卖 $18,000 底线价格 不讲价了 New Jersey& New York City Only
很美的宝蓝色 全皮座椅 电动加热 无限蓝牙 倒车摄像头
2009年10月我买这个车的时候,这个车一直是dealer在开,第一个owner是infiniti公
司。所以车特别新,除了驾驶员皮座椅能看出来有人用过,其他座椅都是全新的。然后
我开这两年,也十分仔细。无吸烟车,没有小孩子和小狗,干干净净,经常去infiniti
店里做保养,有单据可以提供。去年夏天在local交通灯左转,被一个福特车刮了驾驶
员那边的后门边,留了个小坑。对方保险公司只出50%赔偿,朋友们都说不必修,赚那
两千多刀。我心疼车,自己又添了些钱,把整个后门换了新的。但是再就没有别的事故
和刮碰了。
去年冬天花了一千多美金换了4个新轮胎,20英寸大轮胎,4轮驱动,下雪天怎么开都没
问题。绝对安全。而且这个车有s... 阅读全帖
s*******g
发帖数: 243
36
是的。但是这玩意再往里一层想,就是资源管理是怎么做的。Java里面你要在catch
exception,在finally里释放。python跟c#分别有比较方便的with和using。c++怎么做
,就是RAII。尽管因为各个语言的特点导致方式不同,但背后的道理是一样的。c++的
高手转到java,自然想要知道资源是怎么管理的,稍微研究下就知道了。反之从java转
到c++也是如此。写代码,刨除业务逻辑不谈,最后就是这些背后的道理。比如在这里
,背后的道理就是文件,网络链接等等是比较重的资源,用了要尽快释放。万变不离cs
跟se的各种基础。各种语言框架这些各个方面的特点不一样,但背后的道理并不是大相
径庭。
v*s
发帖数: 946
37
来自主题: Linux版 - Google go 还挺不错的
另外俺还喜欢go中的下面这些东西:
强类型系统。 但是编译器又会自动推断你的类型。例如
xx := abc.efg(123)
可以返回多个数值。 a,b := abc.f(123)
defer. 用它来实现 RAII 比 C++ 利用栈上对象的析构函数的方案让人塌实多了
s*******e
发帖数: 28
38
来自主题: Programming版 - 几个问题
Or you can use objects(smart pointers) instead of explicit dynamical
allocations(new/delete) to manage resource. Whenever exceptions are thrown,
those local smart pointers will be destroyed by calling destructors
automatically. This idiom is so called Resource Acquisition is
Initialization(RAII). See effective C++ Item 13.
r*********r
发帖数: 3195
39
来自主题: Programming版 - 有大侠讲讲RTTI和exception的问题么?
i think exception has a more damaging problem.
that is, it will cause memory leak, when the exceptional path is taken.
especially when exceptions are thrown from constructors and destructors.
using RAII may solve the problem if the exception is thrown by constructor.
but the ones thrown by destructors are truly problematic.
w***g
发帖数: 5958
40
java里面要explicitly close。C++里那讨RAII貌似在java里不行(不确信,希望java
高手指正).
python不知道。
a**a
发帖数: 416
41
这有点C++的RAII的味道,一个是程序简洁了,这个优势在同时对几个资源进行操作的
时候
就表现出来了,第二个就是程序上可以形象地表示资源作用的范围。
j****e
发帖数: 140
42
来自主题: Programming版 - 用gc的语言是不是就不能用RAII了?
那些语言里非内存的资源是怎么处理分配和释放的?
r****o
发帖数: 1950
43
来自主题: Programming版 - 用gc的语言是不是就不能用RAII了?
sorry, what is gc?
r*********r
发帖数: 3195
44
unnamed semaphore 应该不涉及系统资源. destroy 就是把里面的 mutex, condition
variable, count 清零, 禁止再对其用p/v操作.
这种有可能涉及资源的, 用 c++ RAII 最安全, dtor 自动释放 whatever needed to
be released.
g*****y
发帖数: 7271
45
来自主题: Programming版 - 大家对 exception 都是怎么处理的?
google raii (so-called Resource Acquisition Is Initialization)

from
m******n
发帖数: 155
46
如果是stack上的内存,不需要清理。
如果是heap上的,分配内存的时候就应该依照RAII的原则,或者用现成的smart
pointer。没这么做的话内存就漏了。
g*****y
发帖数: 7271
47
来自主题: Programming版 - C++ 的 exception handling
常用资源像memory,file之类的,自然有smart pointer,stream
之类的。不常用的资源,去看一看 ScopedGuard 一类的RAII design吧。例如 http://www.ddj.com/cpp/184403758
比finally好用多了。finally 主要是给没有deterministic destructor 的语言用的。
如果用到的资源比较多,还有dependency的话,能给人烦死。
k*******d
发帖数: 1340
48
来自主题: Programming版 - c++ interview: iterator 和 pointer区别?
我印象中iterator对resource allocation没怎么支持吧,它支持RAII?貌似不是,所以
,把他和tr1::shared_ptr等同是不够准确的,当然,如果说广义的smart pointer那的
确有联系
e****d
发帖数: 895
49
来自主题: Programming版 - C++的exception大家常用吗?
I think your question also applies to the exception handling of
other languages. If you are concerned about missing finally statement
or not being able to print out exception stacks, you can solve it by
proper RAII and exception chaining.
d****p
发帖数: 685
50
In C I think it is OK.
In C++, this is not good design since the pointer may be casted to other
types and thus a subsequent free operation may under/over free memory.
C++'s rationale is when you acquire resource, you should immediately make
arrangement to let it be released sometime later, ie RAII. If you really
want a function to allocate memory which should be released in outter scope,
you may consider using shared pointer.
1 2 3 4 下页 末页 (共4页)