- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我了解有关为特定类重载 new 运算符的基础知识。但是,如果可能的话,我不明白一件事。假设我有这样的类(class):
class X{
int a;
long b;
float c;
}
我希望在我的程序一开始就预先创建 100 个 X 对象。我想只调用一次新运算符,分配(至少)(4 + 4 + 4?)x 100 = 1200 字节。然后,每当 X::new()
被调用,而不是 new()
( 或 malloc()
)被调用时,我将返回X
对象的空“外壳”,然后是 a
、b
和 c
被简单地分配给数据成员。
我该怎么做呢?我的问题的重点是当我为 100 个 X 对象保留 1200 个字节时,内存只从内核中获取一次。在我的程序开始后,我希望在检索 X 对象“shell”时执行最低限度的操作?
最佳答案
The emphasis on my question is that memory is only taken from the kernel once, when I reserve 1200 bytes for 100 X objects. After the beginning of my program I want the bare minimum to be executed when retrieving an X object "shell"?
听起来您正在尝试做的事情类似于构建一个 memory pool .
在内存池中,您预先分配了一个大型原始缓冲区,对象最终将存放在该缓冲区中。稍后您将在此缓冲区中分配各个对象。这样做的好处是不必为每个单独的对象分配内存——您所要做的就是在预先分配的空间内构造对象。由于您不必为每个以这种方式实例化的 Object
深入内核,因此可能会节省大量时间。缺点是您负责以更直接的方式管理这些对象。代码可能很棘手、复杂且容易出错。
要分配原始缓冲区,我们只需分配一个足够大的 char
数组来容纳所有预期的 Object
:
char* buf = new char [1200];
为了完成第二部分——在内存池中构造一个对象——你需要使用placement-new。 .假设 buf
是预分配缓冲区中您希望构建新对象 p
的位置:
Object* p = new (buf) Object();
当需要销毁这个对象时,不要使用delete
。 delete
将尝试为对象解除分配内存,从而产生未定义的行为并可能发生崩溃,因为您没有为对象1 分配内存。相反,这是一种您必须直接调用析构函数的情况:
p->~Object();
一旦所有对象都被销毁,您就可以使用delete[]
释放缓冲区:
delete [] buf;
这里是一个完整的例子,展示了如何使用 placement-new
,包括缓冲区的构建。这使用(隐式定义的)默认构造函数。稍后我将展示如何使用另一个构造函数:
#include <cstdlib>
#include <new> // required for placement-new
#include <iostream>
class X
{
public:
int a;
long b;
float c;
};
int main()
{
// construct the memory pool's buffer
char* buf = new char [sizeof(X) * 1000]; // enough memory for 1000 instances of X
// Instantiate 1000 instances of X using placement-new
for (size_t i = 0; i < 1000; ++i)
{
// Where in the memory pool shoudl we put this?
char* buf_loc = buf + (sizeof(X) * i);
// Construct a new X at buf_loc
X* new_x = new (buf_loc) X;
}
// Do something with those instances
for (size_t i = 0; i < 1000; ++i)
{
// Where is the object?
char* buf_loc = buf + (sizeof(X) * i);
X* my_x = reinterpret_cast <X*> (buf_loc); // this cast is safe because I *know* that buf_loc points to an X
// Let's assign some values and dump them to screen
my_x->a = i;
my_x->b = 420000 + i;
my_x->c = static_cast <float> (i) + 0.42;
std::cout << "[" << i << "]\t" << my_x->a << "\t" << my_x->b << "\t" << my_x->c << "\n";
}
// Destroy the X's
for (size_t i = 0; i < 1000; ++i)
{
// Where is the object?
char* buf_loc = buf + (sizeof(X) * i);
X* my_x = reinterpret_cast <X*> (buf_loc); // this cast is safe because I *know* that buf_loc points to an X
// Destroy it
my_x->~X();
}
// Delete the memory pool
delete [] buf;
}
现在让我们为 X
定义一个带参数的构造函数:
class X
{
public:
X (int aa, long bb, float cc)
:
a (aa),
b (bb),
c (cc)
{
}
int a;
long b;
float c;
};
由于我们在这里定义了一个构造函数,编译器将不再为 X
隐式定义一个默认构造函数。
让我们使用这个新的构造函数。现在必须传递构造函数的参数:
// Instantiate 1000 instances of X using placement-new
for (size_t i = 0; i < 1000; ++i)
{
// Where in the memory pool shoudl we put this?
char* buf_loc = buf + (sizeof(X) * i);
// Construct a new X at buf_loc
X* new_x = new (buf_loc) X(0,0,0.0f);
}
不要使用 delete
1:从技术上讲,这里使用 delete
会产生 undefined行为 是因为delete
只能使用从调用new
返回的指针来调用。由于您没有新建
对象,而是放置-新建
对象,因此您不能调用delete
。
关于c++ - 通过一次调用 new 运算符来批量分配对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20524280/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!