我可以分配内存块用新的? [英] Can I Allocate a Block of Memory with new?

查看:141
本文介绍了我可以分配内存块用新的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这样的回答:使用单一​​的malloc到 http://stackoverflow.com/a/19765782/1606345 描述分配结构,并在同一时间的数组。

So this answer: http://stackoverflow.com/a/19765782/1606345 described using a single malloc to allocate a struct and it's arrays at the same time.

我想知道有没有类似的方式与新的做到这一点?结果
很显然,我希望能够分配到,我在运行时收到的是与C的例子做了尺寸。

What I'd like to know is there a similar way to do this with new?
Obviously I want to be able to allocate to a size that I receive at runtime as is done with the C example.

推荐答案

您可以使用分配的内存块与数组字符,然后使用安置新调用该存储器块的构造。

You can allocate a block of memory using new with an array of char, then use placement new to call the constructor on that block of memory.

这篇关于我可以分配内存块用新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆