我最近遇到了这种咆哮。
文章中提到的几点我不是很明白:
- 作者提到了
delete
vs的小烦恼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()'.
在“重载 ::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;
'调试 C++ 中的内存分配'。不能在这里争论。
整篇文章似乎都围绕在自定义内存管理方案中具有重要 构造函数和析构函数的类展开。虽然这可能很有用,而且我无法反驳,但它的共性非常有限。
基本上,我们有放置新的和按类分配器——这些方法不能解决哪些问题?
另外,如果我只是笨拙和疯狂,在你理想的 C++ 中,什么会代替operator new
?必要时发明语法——什么是理想的,只是为了帮助我更好地理解这些问题。