我发现这个代码示例用于研究:
T & T::operator=(T const & x)
{
if (this != &x)
{
this->~T(); // destroy in place
new (this) T(x); // construct in place
}
return *this;
}
Run Code Online (Sandbox Code Playgroud)
当我查看文档时,new没有带指针的版本.从而:
如果我有:
struct a_struct
{
int an_int;
a_struct(int f) : an_int(f) {}
a_struct() : an_int(0) {}
};
class a_class
{
a_struct * my_structs;
a_class() {...}
};
Run Code Online (Sandbox Code Playgroud)
我可以:
a_class() {my_structs = new a_struct(1)}
//or
a_class() {my_structs = new a_struct [10]}
Run Code Online (Sandbox Code Playgroud)
但我不能这样做:
a_class() {my_structs = new a_struct(1) [10]}
//or
a_class() {my_structs = new a_struct() [10]}
Run Code Online (Sandbox Code Playgroud)
有没有正确的语法来使这个工作?或者轻松解决?
我知道我可以new char[n]创建一个n字符数组.即使n不是编译时常量,这也可以工作.
但是让我说我想要一个大小变量后跟n个字符:
我的第一次尝试是:
struct Test
{
std::size_t size;
char a[];
};
Run Code Online (Sandbox Code Playgroud)
然而,似乎new Test[n]没有做我期望的,而是分配n sizes.
我还发现sizeof(std::string)在ideone处是4,所以它似乎可以在一个块中分配size和char数组.
有没有办法实现我所描述的(大概是std::string已经做过的)?
The following example of usage of placement-new was provided by an earlier version of the cppreference page:
char* ptr = new char[sizeof(T)]; // allocate memory
T* tptr = new(ptr) T; // construct in allocated storage ("place")
tptr->~T(); // destruct
delete[] ptr; // deallocate memory
Run Code Online (Sandbox Code Playgroud)
Inspired by this comment on an older SO thread I have come to the conclusion that this might be UB. However, in the talk page of that cppreference article there is a small discussion as …
我需要一个既不可复制也不可移动的元素容器。这些元素不是默认可构造的,但它们的构造函数获得相同的参数。
容器的大小在其生命周期内不会改变。它应该像内置数组一样简单,但它的大小是在运行时调用构造函数时确定的。
有没有一种简单的方法来实现它而不会产生内存分配和使用引起的间接开销std::vector<std::unique_ptr<T>>?
在挖掘C++项目时,我遇到了C++ new运算符的奇怪用法:
int arr[5];
ClassA* a = new(arr) ClassA();
Run Code Online (Sandbox Code Playgroud)
你能帮我理解这个语法吗?
与堆相比,我对堆栈的了解非常简陋,但是当涉及到数组时,从我所知道的内容就是在堆栈上创建的
float x[100];
Run Code Online (Sandbox Code Playgroud)
而这样的东西是在堆上创建的
float* x = new float[100];
Run Code Online (Sandbox Code Playgroud)
但是如果我创建一个模板数组类,并以"堆栈"数组类型(如float[100])传递它会发生什么?例:
#include <iostream>
using namespace std;
template <class T>
class Array {
public:
int size;
T* data;
Array(int size_) : size(size_) {
data = new T[size];
}
~Array() {
delete [] data;
}
};
int main() {
int m = 1000000;
const int n = 100;
Array<float[n]>* array = new Array<float[n]>(m);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) …Run Code Online (Sandbox Code Playgroud) 目前我正在研究自定义内存分配,其中一个缺点是我必须编写多行来实现new-expression只需一个简单调用所提供的相同结果.
简单初始化:
MyClass *obj = new MyClass(3.14);
Run Code Online (Sandbox Code Playgroud)
初始化不太简单:
void *obj_mem = alloc->Allocate(sizeof MyClass, alignof(MyClass));
MyClass *obj = new(obj_mem) MyClass(3.14);
Run Code Online (Sandbox Code Playgroud)
我将为我的项目组提供像那样的分配器,并希望它们实际使用它们,而不是回到调用new,因为我们需要这些更快的分配器来管理我们的内存.
但为了实现这一点,我将不得不设计最简单的语法来使用我的自定义分配器初始化变量.
我最好的选择是覆盖operator new每个类,因为它是new-expression 的分配函数.
class MyClass
{
...
void* operator new(size_t size, Allocator *alloc)
{
return alloc->Allocate(size, alignof(MyClass));
}
}
Run Code Online (Sandbox Code Playgroud)
然后初始化变量的语法成为我最终想要的:
MyClass *obj = new(alloc) MyClass(3.14);
Run Code Online (Sandbox Code Playgroud)
但是,如果我能得到与上述相同的一般信息,那就太好了.所以我不必operator new为每个班级重写.
请参阅以下代码:
#include<iostream>
#include<stdlib.h>
using namespace std;
class ex
{
int i;
public:
ex(int x){
i=x;
cout<<"\nconstructor";
}
void setval(int x)
{
i=x;
}
int geti(){return i;}
~ex()
{
cout<<"\ndestructor";
}
};
int main()
{
ex *ob;
ob=(ex*) malloc(sizeof(ex));
ob->setval(5);
cout<<ob->geti();
delete ob;
}
Run Code Online (Sandbox Code Playgroud)
我以为上面的代码会显示错误,但它编译成功并显示输出
5
destructor
Process returned 0 (0x0) execution time : 0.270 s
Run Code Online (Sandbox Code Playgroud)
我的问题是:
我可以使用 为对象分配内存malloc吗?
如何malloc为没有构造函数参数的类分配内存?
我可以malloc()用于分配和delete解除分配还是new用于分配和free()解除分配?
没有构造函数如何调用析构函数?
c++ ×10
arrays ×3
new-operator ×2
c++11 ×1
c++14 ×1
c++17 ×1
constructor ×1
containers ×1
heap-memory ×1
noncopyable ×1
stack-memory ×1
struct ×1
templates ×1
visual-c++ ×1