相关疑难解决方法(0)

新(这)意味着什么?

我发现这个代码示例用于研究:

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没有带指针的版本.从而:

  1. 新(这)意味着什么?
  2. 它是干什么用的?
  3. 如果未在文档中列出,如何调用它?

c++

11
推荐指数
1
解决办法
1519
查看次数

C++:使用非默认构造函数动态分配结构的成员数组

如果我有:

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)

有没有正确的语法来使这个工作?或者轻松解决?

c++ arrays constructor struct memory-management

8
推荐指数
1
解决办法
3579
查看次数

使用可变长度数组成员分配struct

我知道我可以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已经做过的)?

c++ new-operator dynamic-memory-allocation c++11

8
推荐指数
3
解决办法
2万
查看次数

UB when deleting storage-providing char array from free store?

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 …

c++ placement-new undefined-behavior language-lawyer

8
推荐指数
0
解决办法
101
查看次数

具有不可复制不可移动元素类型的 C++ 容器

我需要一个既不可复制也不可移动的元素容器。这些元素不是默认可构造的,但它们的构造函数获得相同的参数。

容器的大小在其生命周期内不会改变。它应该像内置数组一样简单,但它的大小是在运行时调用构造函数时确定的。

有没有一种简单的方法来实现它而不会产生内存分配和使用引起的间接开销std::vector<std::unique_ptr<T>>

c++ arrays containers noncopyable

7
推荐指数
1
解决办法
1682
查看次数

c ++ placement new和overloading new

关于SO的许多问题都询问了C++的新特性(例1,例2)为什么要使用它.许多答案都说 - 自定义分配对象,如预先分配的空间.

但问题是 - 为什么需要为此安排新的?不仅仅是运营商的新超载足够吗?通过重载运算符new for class I可以精确控制从哪里获取内存 - 比如调用自定义分配器.那么为什么我需要为此目的安置新的?

c++ memory-management operator-overloading

6
推荐指数
2
解决办法
3235
查看次数

奇怪的C++新运算符用法

在挖掘C++项目时,我遇到了C++ new运算符的奇怪用法:

int arr[5];
ClassA* a = new(arr) ClassA();
Run Code Online (Sandbox Code Playgroud)

你能帮我理解这个语法吗?

c++ new-operator

6
推荐指数
1
解决办法
722
查看次数

数组作为模板参数:堆栈还是堆?

与堆相比,我对堆栈的了解非常简陋,但是当涉及到数组时,从我所知道的内容就是在堆栈上创建的

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)

c++ arrays templates heap-memory stack-memory

6
推荐指数
1
解决办法
1188
查看次数

如何在函数调用之后初始化变量,就像new-expression提供的那样?

目前我正在研究自定义内存分配,其中一个缺点是我必须编写多行来实现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为每个班级重写.

c++ visual-c++ c++14 c++17

6
推荐指数
1
解决办法
341
查看次数

我可以使用 malloc 为类对象分配内存吗?

请参阅以下代码:

 #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)

我的问题是:

  1. 我可以使用 为对象分配内存malloc吗?

  2. 如何malloc为没有构造函数参数的类分配内存?

  3. 我可以malloc()用于分配和delete解除分配还是new用于分配和free()解除分配?

  4. 没有构造函数如何调用析构函数?

c++

6
推荐指数
2
解决办法
1187
查看次数