C++ STL问题:分配器

发布于 2024-07-04 16:31:46 字数 109 浏览 6 评论 0原文

我有一个关于 C++ STL 的(可能是愚蠢的)问题。 当我创建一个容器(向量、集合、映射等)时,它是在堆栈上还是在堆上分配? 如果我做了一个集合并放入了 500 万个字符串,我是否需要担心堆栈溢出?

I have a (potentially dumb) question about the C++ STL. When I make a container (vector, set, map, etc), is it allocated on the stack or on the heap? If I make a set and put 5 million strings, will I have to worry about a stack overflow?

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

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

发布评论

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

评论(3

音栖息无 2024-07-11 16:31:47

默认情况下,STL 类从堆中分配其内部缓冲区,尽管这些类还允许自定义分配器,允许用户指定要从中分配的备用位置(例如共享内存池)。

STL classes by default allocate their internal buffers from the heap, although these classes also allow custom allocators that allow a user to specify an alternate location to allocate from - e.g. a shared memory pool.

要走干脆点 2024-07-11 16:31:47

STL 容器的默认分配器使用operator new 和delete,因此它是所包含类型的路由。 (一般来说,它来自堆,除非您执行某些操作来覆盖它。)

分配 500 万个字符串不会导致堆栈溢出。 即使您创建了一个基于堆栈的分配器,它也可能在您插入一个字符串之前就溢出。

The default allocator for STL containers uses operator new and delete, so it's whatever those route to for the type being contained. (In general, it comes from the heap unless you do something to override that.)

You will not get a stack overflow from allocating 5 million strings. Even if you made a stack based allocator, it would probably overflow before you even inserted one string.

ヅ她的身影、若隐若现 2024-07-11 16:31:47

容器本身分配在您决定的位置(可以是堆栈、堆、对象的成员等),但默认情况下,正如其他人所述,它使用的内存是在免费存储中获取的(通过 new 和 delete 管理)这与堆不同(通过 malloc/free 管理)。

不要混合两者!

The container itself is allocated where you decide (it can be the stack, the heap, an object's member, etc) but the memory it uses is, by default, as others described, taken on the Free Store (managed through new and delete) which is not the same as the heap (managed through malloc/free).

Don't mix the two!

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