在 C++ 中创建对象不使用“新”

发布于 2024-09-24 03:09:56 字数 212 浏览 3 评论 0原文

我想做一个程序,可以说代表一个矩阵 现在矩阵将由向量表示,向量中的每个对象将 代表一个细胞示例:向量 现在,在构造矩阵时,构造函数会接收要插入矩阵中的单元格列表。 列表的大小在编译时未知

我有兴趣在不使用堆上内存的情况下创建这个矩阵。换句话说,不使用“new”或“delete”一词创建对象 如果我不知道要插入向量中的对象有多少个,有什么办法可以做到这一点?

I want to make a program that lets say represents a matrix
now the matrix will be represented by a vector that each object in the vector will
represent a cell example: vector
now when constructing the matrix the constructor receives a list of cells to insert in the matrix. The size of the list is unknown in compilation time

I am interested in creating this matrix without using memory on the heap. In other words not creating object using the word "new" or "delete"
is there any way to do that if I don't know how many objects are meant to be inserted into the vector?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暖风昔人 2024-10-01 03:09:56

有一种特殊的方法可以使用 new 在堆栈中分配内存或使用所谓的“放置 new 运算符”作为静态存储。使用此版本的 new,您可以保留一块内存,并明确告诉 new 您要在哪里存储特定变量。其工作原理如下:

   #include <new>
   int main()
   {
      char buffer[500];  // chunk of memory
      int p*;
      p = new (buffer) int[10];
   }

请注意,您需要包含 new 标头才能使用这个特殊的 new 运算符。在这种情况下,当您使用自动存储时,内存将在离开声明它的块(主块)时被释放。

参考文献:C++ Primer plus。第 9 章第 420 页

There is a special way to use new to allocate memory in the stack or as static storage using what is called the placement new operator. With this version of new, you reserve a chunk of memory and the you explicitly tell new where you want to store a specific variable. It would work as follows:

   #include <new>
   int main()
   {
      char buffer[500];  // chunk of memory
      int p*;
      p = new (buffer) int[10];
   }

Note that you need to include the new header in order to use this special new operator. In this case, as you are using automatic storage, the memory will be freed upon leaving the block in which it was declared (the main).

References: C++ Primer plus. Chapter 9. Page 420

一梦浮鱼 2024-10-01 03:09:56

如果不使用汇编指令对程序/函数的堆栈帧进行直接(因此依赖于平台)操作,则没有标准方法可以做到这一点 - 我强烈反对这样做。是什么阻止你使用堆?

There is no standard way to do this without performing direct (and thus platform-dependent) manipulation of the program's/function's stack frame using assembly instructions - which I would heartily discourage. What's stopping you from using the heap?

温柔嚣张 2024-10-01 03:09:56

使用alloca获取指针,然后使用就地new运算符:

void *p = alloca(sizeof(Class));
new (p) Whatever(arguments);

但是,do阅读alloca手册使用前请先查看页面! 要非常小心。正如 Jim Brissom 所说,alloca 不可移植。

您不需要删除。当函数返回时,内存将被释放

Use alloca to obtain a pointer and then use the in-place new operator:

void *p = alloca(sizeof(Class));
new (p) Whatever(arguments);

However, do read alloca manual page before using it! Be very careful. As Jim Brissom says, alloca isn't portable.

You don't need to delete. The memory will be freed when the function returns

策马西风 2024-10-01 03:09:56

有一种方法,它非常有限并且非常非正统。您需要创建一个静态大小的 unsigned char 数组,以形成内存池。对象列表的大小会有限制。您需要为该类重载一个 new 运算符(和 delete 运算符),以专门针对此类内存池。

也就是说,确实没有充分的理由走这条路。

There is a way, it's very limiting and very unorthodox. You'll need to create a statically sized array of unsigned char which form a memory pool. There will be a limit to the size of the list of objects. You'll need to overload a new operator (and delete operator) for that class to specifically target such a memory pool.

That said, there's really no good reason to go this route.

孤单情人 2024-10-01 03:09:56

好吧,如果你不想使用堆上的内存,你还想从哪里获取它呢?

a) 系统相关 - 您可以要求操作系统为您分配一些内存。但这是不好的风格(取决于系统),并且将使用相同的 RAM...只是以不同的方式分配。例如,如果您确实有兴趣这样做,Windows 32 中的 ::GlobalAlloc 或 ::LocalAlloc 将执行此类操作。

b) 内存映射文件 - 如果您问这个问题,这可能会很有趣,因为您认为没有足够的可用 RAM 并且访问时间不是问题。

c) 求助于 malloc/free 等 C 函数并强制转换指针...即从堆中获取内存,只是避免使用“new”和“delete”关键字。

然而,如果没有为什么你想要避免新建/删除的信息,很难说出什么是“好的”解决方案。
您需要动态内存分配,这两个工具就是做到这一点的工具。

您能否解释/重新表述您的问题,以便获得更准确的答案?

Well, if you don't want to use memory on the heap, where else do you want to get it from?

a) system dependant - you can ask the operating system to allocate some memory for you. But this is bad style (system dependant), and will use the same RAM... just in a different way allocated. For example, ::GlobalAlloc or ::LocalAlloc in Windows 32 will do such things if you are really interested in doing that.

b) memory mapped files - that might be interesting if you are asking because you think you'll have not enough RAM available and access time isn't an issue.

c) resort to C functions like malloc/free and cast the pointers... that is getting memory from the heap, just avoiding the "new" and "delete" keywords.

However, it is hard to tell what a "good" solution without information why you want to avoid new / delete.
You have need for dynamic memory allocation, these two are the tools to do that.

Could you please explain/rephrase your question so you can get more precise answers?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文