zil*_*n01 1 c++ language-features allocation new-operator
我最近遇到过这种咆哮.
我不太明白文章中提到的一些要点:
deletevs 的小烦恼delete[],但似乎认为它实际上是必要的(对于编译器),而没有提供解决方案.我错过了什么?在"专业分配器"一节中,在功能中f(),似乎可以通过以下方式替换分配来解决问题:(省略对齐)
// if you're going to the trouble to implement an entire Arena for memory,
// making an arena_ptr won't be much work. basically the same as an auto_ptr,
// except that it knows which arena to deallocate from when destructed.
arena_ptr<char> string(a); string.allocate(80);
// or: arena_ptr<char> string; string.allocate(a, 80);
arena_ptr<int> intp(a); intp.allocate();
// or: arena_ptr<int> intp; intp.allocate(a);
arena_ptr<foo> fp(a); fp.allocate();
// or: arena_ptr<foo>; fp.allocate(a);
// use templates in 'arena.allocate(...)' to determine that foo has
// a constructor which needs to be called. do something similar
// for destructors in '~arena_ptr()'.
Run Code Online (Sandbox Code Playgroud)在'Overgers of overloading :: operator new []'中,作者试图做一个new(p) obj[10].为什么不这样(更不含糊):
obj *p = (obj *)special_malloc(sizeof(obj[10]));
for(int i = 0; i < 10; ++i, ++p)
new(p) obj;
Run Code Online (Sandbox Code Playgroud)'在C++中调试内存分配'.不能在这里争论.
整篇文章似乎围绕着具有位于自定义内存管理方案中的重要 构造函数和析构函数的类.虽然这可能有用,但我无法与它争论,但它的共性非常有限.
基本上,我们有新的和每个类的分配器 - 这些方法无法解决哪些问题?
另外,如果我只是粗俗和疯狂,在你理想的C++中,会取代operator new什么呢?根据需要创建语法 - 什么是理想的,只是为了帮助我更好地理解这些问题.
好吧,理想的可能是不需要删除任何类型.有一个垃圾收集环境,让程序员避免整个问题.
咆哮中的抱怨似乎归结为
他说得对,烦人的事实,你必须同时实现new和new[],但你被迫通过Stroustrups的愿望,以保持C'S语义的核心.由于无法从数组中指出指针,因此必须自己告诉编译器.你可以解决这个问题,但这样做意味着从根本上改变语言C部分的语义; 你再也无法利用这个身份了
*(a+i) == a[i]
Run Code Online (Sandbox Code Playgroud)
这将破坏所有C代码的一个非常大的子集.
所以,你可以有一种语言
实现了一个更复杂的数组概念,消除了指针算术的奇迹,用dope矢量或类似的东西实现数组.
垃圾收集,所以你不需要自己的delete纪律.
也就是说,你可以下载Java.然后,您可以通过更改语言来扩展它
void *删除类型检查upcast,...但这意味着您可以编写将Foo转换为Bar的代码而无需编译器查看.如果你需要,这也可以实现鸭式.
问题是,一旦你完成了这些事情,你就拥有了带有C-ish语法的Python或Ruby.
我一直在编写C++,因为Stroustrup发出了cfront 1.0的磁带; C++中涉及的很多历史,因为它现在源于希望拥有适合C世界的OO语言.还有很多其他更令人满意的语言,就像艾菲尔一样,大约在同一时间出现.C++似乎赢了.我怀疑它赢了,因为它可以适应C世界.
| 归档时间: |
|
| 查看次数: |
2536 次 |
| 最近记录: |