调用堆栈上大量对象的构造函数
我正在修改一些 C++ 源代码,我注意到作者确实不遗余力地分配了堆栈上的所有内容。最有可能的是释放好处(还有性能好处吗??)。
我想保持相同的一致性,但我需要创建大量对象,例如:
Object os[1000] = {Object(arg), Object(arg), ....};
不会削减它。周围搜索似乎有一种解决方法:
vector<Object> os(1000, Object(arg));
这仍然在堆上分配,但像堆栈一样释放(从我在其他帖子中读到的内容)。我只是想知道是否还有其他选项,因为这似乎是一个语法问题。也许聪明的#define人都知道。
I'm modifying some C++ source code and I've noticed the author really went out of their way to allocate everything on the stack. Most likely for the deallocation benefits (are there any performance benefits as well??).
I want to keep the same consistency but I need to create a large array of objects and something like:
Object os[1000] = {Object(arg), Object(arg), ....};
isn't going to cut it. Searching around it seems like a way around this is just:
vector<Object> os(1000, Object(arg));
This still allocates on the heap but deallocates like a stack (from what I've read in other posts). I'm just wondering are there any other options because this just seems like a syntax issue. Perhaps a clever #define people know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
堆栈不应该用于大内存块。您只需支付更高的堆分配价格即可换取访问更多内存的好处。另一种选择是声明一个具有静态存储持续时间的数组,但这有其他缺点(不可重入,不是线程安全的)。一切都是一个权衡。
无论如何,在分配复杂对象时,调用 1000 个构造函数的成本将使分配器中花费的时间相形见绌。除非您有显示性能问题的分析器数据,否则只需使用
std::vector
即可。The stack shouldn't be used for large blocks of memory. You simply have to pay the higher price of heap allocation in exchange for the benefit of accessing more memory. Another option is declaring an array with static storage duration, but that has other drawbacks (not re-entrant, not thread-safe). Everything is a tradeoff.
In any case, when allocating complex objects, the cost of calling 1000 constructors will dwarf the time spent in the allocator. Just use
std::vector
unless you have profiler data that shows a performance problem.是的,还有其他选择。您可以使用诸如
alloca
之类的东西。这将使您获得堆栈分配和自动释放,但不会自动构建或销毁。您需要使用新的放置和析构函数的显式调用。是的,可能有性能优势,但你也要求破坏堆栈,并且这种模式不像向量解决方案那样是异常安全的(也就是说,如果你分配的对象有一个不平凡的析构函数)。
Yes, there are other options. You can use something like
alloca
. This will get you stack allocation and automatic free, but not automatic construction or destruction. You would need to use placement new and explicit invocation of the destructors.Yes, there may be a performance advantage, but you're also begging to blow the stack, and this pattern is not exception safe like the
vector
solution would be (that is, if the object your allocating has a non-trivial destructor).一般来说,在堆栈上分配大量数据是一个坏主意。大多数操作系统上的堆栈都是暂存空间,并且大小相当有限。为对象分配大量堆栈空间可能会快速消耗所有可用的堆栈空间,当尝试在堆栈上再分配一项内容(例如,函数调用的返回地址)时,会导致段错误或其他异常。
至于其他选项,您有一些.. std::vector 正如您已经注意到的那样,以及 boost::array 都是此类示例。
Allocating large amounts of data on the stack is, generally speaking, a bad idea. The stack on most operating systems is a scratch space and fairly limited in size. Allocating a large amount of stack space for objects can quickly consume all your available stack space, resulting in a segfault or other exception when something attempts to allocate just one more thing on the stack (for instance, a return address for a function call).
As far as other options, you have a few.. std::vector as you've already noticed, along with boost::array are to such examples.
这应该可行:
这会创建数组,初始化一个对象,然后循环,用最后一个元素初始化每个元素。
当然,您可能不应该使用这个。即使它有效,即使
Object os[1000]
不会给您带来问题,这似乎也是一个坏主意。This ought to work:
This creates the array, initializes one object, then loops through, initializing each element with the last one.
Of course, you probably shouldn't use this. It seems like a bad idea even if it works, and even if
Object os[1000]
doesn't cause you problems.