如何知道正确的最大尺寸的矢量? max_size()?但不是 [英] How to know the right max size of vector? max_size()? but no

查看:153
本文介绍了如何知道正确的最大尺寸的矢量? max_size()?但不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用向量时,内存不足显示。

要修复它,我使用max_size()检查,然后保留或push_back。

如果max_size是更大的保留值,它应该是确定,但它是没有!那么max_size()的含义是什么?

我在Windows 7和Visual studio 2010中编译下面的演示。我的电脑有4GB RAM。当reseverd是max_size()的1/2时,它失败。

When using vector, "Out of Memory" show.
To fix it, I use max_size() to check, then reserve or push_back.
If the max_size() is bigger the reserved value, it should be ok, but it is no! Then what's the meaning of max_size()?
I compile below demo in windows 7 and Visual studio 2010. My PC has 4GB RAM. When the reseverd is 1/2 of max_size(), it fails.

max_size()=2^32/sizeof(CPoint3D)-1=268435455

在演示中的max_size()的1/4时就可以了。

It's ok when 1/4 of max_size() in the demo. In my real project, It's ok until 1/10.

向量最大尺寸的权利是什么,如何放大

我把很多元素推入 std ::时出现了向量。为了避免错误,我使用 vector :: max_size()检查,并使用 vector :: reserve()以预分配存储器。但是,它不工作。在一个演示项目中,当我保留 max_size 的1/4时发生错误。在实际项目中,当我保留1/10的时候会发生错误。我运行Windows 7并使用Visual Studio 2010编译。我的计算机有4GB RAM。

I got the "out of memory" error when I push a lot of elements into std::vector. To avoid the error, I checked with vector::max_size() first, and use vector::reserve() to pre-allocate the memory. However, it doesn't work. In a demo project, the error occurs when I reserve 1/4 of the max_size. In the real project, the error occurs when I reserve 1/10 of it. I'm running Windows 7 and compiling with Visual Studio 2010. My computer has 4GB RAM.

如果 max_size 工作,我如何找出可以为向量分配的元素的最大数量?

If max_size doesn't work, how do I find out the maximum number of elements I can allocate for a vector?

推荐答案

max_size()返回向量可能拥有的元素的最大数量。也就是说,使用可能存储的整数类型和操作系统的地址空间限制来考虑类似寻址限制的情况下的绝对限制。

max_size() returns the maximum number of elements a vector can possibly hold. That is, the absolute limit when taking into account things like the addressing limits using the integral types it might store and the address space limits of the operating system.

t意味着你可以使一个向量拥有许多元素。它只是意味着你永远不能存储更多。也只是因为你有4 gig的RAM并不意味着你可以创建一个单一的连续缓冲区,占用4 gig RAM或任何附近。还有其他因素要考虑像内存碎片(你可能只能将一个gig内存块寻址到物理内存中)。

This doesn't mean you can actually make a vector hold that many elements. It just means you can never store more. Also just because you have 4 gigs of RAM doesn't mean you can actually create a single contiguous buffer that occupies 4 gigs of RAM or anywhere close. There are other factors to consider like memory fragmentation (you might only be able to page a one gig memory block into physical memory due to it).

如果你真的需要这个许多元素在容器中,连续序列可能不是一个好的选择。对于大的数据集,您可能需要一些可以分页的数据,比如std :: deque。

If you really need this many elements in a container, a contiguous sequence is probably not a good choice. For data sets that large, you may need something that can be paged in bits and pieces like std::deque.

这篇关于如何知道正确的最大尺寸的矢量? max_size()?但不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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