- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何使用 placement new 运算符创建对象数组?我知道如何从其他 SO 问题中为单个对象执行此操作。但是我找不到对象数组。
为了查看性能差异,我尝试创建一个对象数组并在子循环之后删除。但我找不到办法。如何创建多个对象?
class Complex
{
double r; // Real Part
double c; // Complex Part
public:
Complex() : r(0), c(0) {}
Complex (double a, double b): r (a), c (b) {}
void *operator new( std::size_t size,void* buffer)
{
return buffer;
}
};
char *buf = new char[sizeof(Complex)*1000]; // pre-allocated buffer
int main(int argc, char* argv[])
{
Complex* arr;
for (int i = 0;i < 200000; i++) {
//arr = new(buf) Complex(); This just create single object.
for (int j = 0; j < 1000; j++) {
//arr[j] = Now how do I assign values if only single obect is created?
}
arr->~Complex();
}
return 0;
}
最佳答案
将标准定义的新运算符覆盖为相当无用的函数的目的是什么?以及如果你一个一个地创建你应该如何存储指针
#include <iostream>
class Complex
{
double r; // Real Part
double c; // Complex Part
public:
Complex() : r(0), c(0) {}
Complex (double a, double b): r (a), c (b) {}
};
char *buf = new char[sizeof(Complex)*1000]; // pre-allocated buffer
int main(int argc, char* argv[])
{
// When doing this, it's _your_ problem
// that supplied storage is aligned proeperly and got
// enough storage space
// Create them all
// Complex* arr = new(buf) Complex[1000];
Complex** arr = new Complex*[1000];
for (int j = 0; j < 1000; j++)
arr[j] = new (buf + j*sizeof(Complex)) Complex;
for (int j = 0; j < 1000; j++)
arr[j]->~Complex();
delete[] buf;
return 0;
}
如果您要根据 placement new 设计任何基础设施,您很可能需要使用 RAII 创建一个工厂类来构造和存储对象并控制缓冲区。这样的类\模式通常被称为内存池。我见过的唯一实用的池实现是存储数组和不同大小和类型的类,使用特殊类来存储对此类对象的引用。
关于c++ - 如何使用 placement new 运算符创建对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42771709/
我看过一些 placement new 的例子,但对各种类型内部发生的事情有点困惑。 一个简单的例子: using my_type = std::string; using buffer_
需要帮助在 Hibernate 继承中实现此场景 Cow 和 Dog 继承 Animal 类, 牛和狗都有“后代”名单 牛有额外的 list - 每月疫苗接种日期 这个 Hibernate 表示方式正
背景 我正在开发一个嵌入式 C++ 项目,在该项目中我正在使用继承构建一个简单的状态机。这个想法是每个状态都将继承自一个名为 State 的基类,状态管理器将在状态转换期间创建和销毁子类实例。为了避免
这里有没有人使用过C++的“placement new”?如果是这样,那是为了什么?在我看来,它仅在内存映射的硬件上有用。 最佳答案 新的Placement允许您在已分配的内存中构造一个对象。 当您需
我一直在阅读一些关于 placement new on SO 的问题,我发现了一些不同的使用方法,我想我会在这里问一下。我知道 placement new 基本上是您创建对象并在您选择的已分配内存中为
我正在通过以下代码学习新的 C++ 布局。 class Cell { public: Cell() { printf("default constructor by %s\n"
#include #include struct A { int a; }; struct B : virtual A { int b; }; struct C : virtual A { in
我刚刚发现了一些用 C++ 实现的容器。该类使用内部缓冲区来管理其对象。这是一个没有安全检查的简化版本: template class Container { public: Containe
是否有一种模式可以在退出范围时自动调用堆栈上 placement-new 初始化对象的析构函数?我想跳过内存显式调用析构函数的需要。或者,是否有一种不同于 placement-new 的方法来构建具有
在 Android Activity 上,我想将图像设置为左下角的背景。操作方法如下:Background Image Placement 但是,我还希望能够指定不是该图像的其余背景具有特定颜色。这可
这里有人用过C++的“placement new”吗?如果是这样,为什么?在我看来它只对内存映射硬件有用。 最佳答案 Placement new 允许您在已分配的内存中构造一个对象。 当您需要构造一个
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
假设我有一个 MyStack 类公开了: class MyStack { public: template T* Push() { Reserve(sizeof(T)); // Make s
这里有人用过C++的“placement new”吗?如果是这样,为什么?在我看来它只对内存映射硬件有用。 最佳答案 Placement new 允许您在已分配的内存中构造一个对象。 当您需要构造一个
这里有人用过C++的“placement new”吗?如果是这样,为什么?在我看来它只对内存映射硬件有用。 最佳答案 Placement new 允许您在已分配的内存中构造一个对象。 当您需要构造一个
我正在创建一个受歧视的类 union 体。我使用的是 c++ 17,所以我可以在技术上使用 std::variant,但由于特定的用例,我希望每个变体的含义更加明确(特别是因为其中两种情况没有数据,除
尝试使用 placement new 但它一直给我错误。我记得不久前,它正在工作。 g++(版本 4.8.4)在 Ubuntu 14.04 上。 #include typedef unsigned
这里有人用过C++的“placement new”吗?如果是这样,为什么?在我看来它只对内存映射硬件有用。 最佳答案 Placement new 允许您在已分配的内存中构造一个对象。 当您需要构造一个
这个问题在这里已经有了答案: What uses are there for "placement new"? (25 个答案) 关闭 8 年前。 我有两个使用new运算符分配内存的案例。 clas
我有一 block 内存区域,将用于多个队列。比如我分配了1024字节的内存,需要两个队列。第一个队列将占用前 512 个字节,第二个队列将占用接下来的 512 个字节。 但是,我的队列由 C++ 类
我是一名优秀的程序员,十分优秀!