【问题标题】:How to create object in heap without using new operator in c++?如何在堆中创建对象而不使用 C++ 中的 new 运算符?
【发布时间】:2017-03-08 07:14:55
【问题描述】:

new C++ 中的运算符用于在堆内存中创建新对象。我不知道如何在不使用 new 运算符的情况下在堆内存中创建对象。有可能吗,怎么做?

还有人可以建议我在 C++ 中使用 new opeartor 在堆栈中创建对象吗?

【问题讨论】:

  • 你为什么需要那个?
  • “还有人可以建议我使用 c++ 中的新操作符在堆栈中创建对象吗?”您不使用 new 创建堆栈分配的对象。
  • @πάνταῥεῖ 可以使用placement new。
  • 我认为这是一个可以回答的问题,答案相当简短,分为两部分:第一部分使用 malloc 并放置新位置,带有关于 malloc 的适当警告,第二部分是如何分配缓冲区在堆栈中(例如作为固定大小char[])并使用放置新的堆栈空间。
  • C++ 并没有真正定义“堆栈”和“堆”是否也值得吹毛求疵? (标准只使用“自动”、“静态”、“动态”等存储时长)

标签: c++ c++11 memory-management c++14 new-operator


【解决方案1】:

根据我对 C++ 的有限经验,使用 new 运算符是在堆中创建公共对象的唯一方法。 HeapAlloc 可以在堆中分配内存,但是如何不使用 new 操作符在堆中创建类呢?

【讨论】:

  • HeapAlloc 也是不可移植的——它是 Windows API 的一部分。 C 风格 malloc 应该是可移植的,真正的问题只是 为什么 你想在 new 上使用它
  • @xingfu0539 这不准确。分配有newstatic C 数组将存在于堆上。否则,如果在函数范围内,则在堆栈上分配。
  • 显然malloc 在 C++ 中工作并且是在许多分配器中实现operator new 的主要方式。另一方面,您需要在此之后放置 new 以实际创建一个重要的对象。
  • @nikaza:这个评论没有意义。如果它是一个static C 数组,它不会分配在堆上,也不会分配给new[];如果它在堆上分配有new[] / 它不是静态的。这是一个或另一个。
【解决方案2】:

在现代C++ 中,通常通过使用返回智能指针的函数std::make_uniquestd::make_shared 来避免直接使用new

例如。

class Object
{
public:
    Object(int i): i(i) {}
    // ...
    void do_stuff() { /*...*/ }

private:
    int i;
};

auto object = std::make_unique<Object>(4);

object->do_stuff();

现在您不必担心删除object

【讨论】:

    【解决方案3】:

    要回答您的一个问题,您可以使用placement new 在堆栈上分配对象。例如:

    int main()
    {
        using namespace std;
    
        char buf[sizeof(string)];                // allocate char array on stack
        string *str = new (buf) string{ "I'm a placement new string object" };
    
        cout << *str << endl;
    
        str->~string();                          // delete string object
                                                 // character array object will be
                                                 // automatically deallocated
    
        return 0;
    }
    

    【讨论】:

      【解决方案4】:

      你可以在c++中使用c语言创建对象而不使用new关键字。你可以使用malloc创建一个指针来分配到堆内存中的地址:

      // make sure to include iostream to access the c library so you can use malloc
      #include <iostream>
      class Rectangle
      {
      private:
          int length;
          int breadth;
      
      
      public:
      
          Rectangle(int l, int b) :length(l), breadth(b)
      {
          std::cout << "overloaded constructor called!" << std::endl;
      }
      
      void set_length(int length) 
      {
          length = length;
      }
      
      void set_breadth(int breadth)
      {
          breadth = breadth;
      }
      
      int area() {
          return length * breadth;
      }
      
      int get_length() 
      {
          return length;
      }
      
      int get_breadth()
      {
          return breadth;
      }
      
      
      
      };
      
      int main(int argc, char const *argv[])
      {
      // create a pointer object
          Rectangle *ptr;
      // allocate object to heap memory using malloc
          ptr = (Rectangle *) malloc(sizeof(Rectangle));
          ptr->set_length(10);
          ptr->set_breadth(15);
      
          printf("length %d\n", ptr->get_length());
          printf("breadth %d\n", ptr->get_breadth());
      
      // free up heap memory when done using delete keyword
          delete ptr;
      
          return 0;
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-13
        • 1970-01-01
        • 2015-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多