释放堆栈上的空间 [英] Freeing up space on the stack

查看:92
本文介绍了释放堆栈上的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以如果你在堆栈上有一堆int和string的巨大向量,这会导致任何问题吗?



另外,你如何释放一个矢量对象矢量<对象>



你只需要编写vectorName.clear()吗?

解决方案

< blockquote>是的,它可以,如果堆栈滥用(或错误)。堆栈溢出(不是由于无限递归,这是最常见的事情)是不太可能的,但C ++有一些额外的能力来过度使用堆栈,只是因为它的类/结构语义允许轻松使用相同的大数据结构在堆和堆栈上以非常相似的方式。



你不应该实现它。对于任何携带海量数据的类型,包括大数组,列表,哈希表,其他具有海量数据的集合结构,请确保将它们放在堆上,堆栈内存应该只有指向堆上的数据结构的指针。



-SA


So if you have a bunch of huge vectors of int and string on the stack, can that cause any problem?

Also, how do you free up a vector of object vector<Object>

Do you just have to write vectorName.clear()?

解决方案

Yes, it can, in case of stack abuse (or bugs). The stack overflow (not due to "infinite" recursion, which is a most usual thing) is very unlikely, but C++ has some extra ability to overuse the stack, just because its class/struct semantic allows to easily use the same big data structures on both heap and stack in a very similar way.

You just should not make it happen. For any type carrying massive data, including big arrays, list, hash tables, other collection structures with massive data, make sure you put them on heap, and the stack memory should have only the pointers to those data structures on heap.

—SA


这篇关于释放堆栈上的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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