向量初始化期间未知的应用程序失败 [英] Unknown application failure during vector initialization

查看:56
本文介绍了向量初始化期间未知的应用程序失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按如下方式初始化向量:

I am trying to initialise a vector as follows:

vector<int> vect(n, 2);

此语句初始化vect以包含值为2int类型的n元素. n是类型为size_t的变量,已经过检查以确保n <= vect.max_size()始终为真.

This statement initialises vect to contain n elements of type int with value 2. n is a variable of type size_t that has been checked to ensure n <= vect.max_size() is always true.

到目前为止,我目前正在Code :: Blocks上测试我的代码.它适用于n的任意小值,但是当我尝试在IDE中的n = vect.max_size()上运行它时,程序终止,并说发生了未知的应用程序错误,并与开发人员(me)交谈.

I am currently testing my code so far on Code::Blocks. It works for any arbitrary small value of n, but when I try to run it in the IDE at n = vect.max_size(), the program terminates, saying an unknown application error occurred, and to speak to the developer (me).

有人知道出什么问题了吗?这不是在运行时将向量初始化为最大大小的正确方法吗?我检查了一下,我的数学是正确的,n没有超过vect.max_size()

Does anyone know what is wrong? Is this not the right way to initialise the vector to the maximum size possible in runtime? I checked, my math is correct, n didn't exceed vect.max_size()

推荐答案

max_size()成员在一定程度上是理论上的限制.在具有大量内存的机器上,可以创建具有相应大小的容器,但是现代技术通常无法实现.

The max_size() members are somewhat theoretical limits. On machines with large amounts of memory it may be possible to create containers with the corresponding size but typically it won't be possible with modern technology.

max_size()存在的原因在历史的迷雾中几乎消失了.我怀疑,当开发STL并使用小"指针实例化容器(类似于__small int*)时,该值具有一定意义:在那时,某些平台的指针类型不同大小,例如16位的小"指针和32位的大"指针.

The reason for the existence of max_size() is pretty much lost in the mists of history. I suspect that the value was sort of meaningful when STL was developed and a container was instantiated with a "small" pointer (something like __small int*): at around that time some platforms had pointer types of different size, e.g., a 16 bit "small" and a 32 bit "large" pointer.

我认为没有一种可行的方法来确定可以分配的容器的最大大小.充其量,您也许可以执行类似二进制搜索的操作,尝试创建具有一定大小的容器并捕获std::bad_alloc:最有可能是您的应用程序错误"和该类型的未捕获异常.

I don't think there is a practical way to determine the maximum size of a container which can be allocated. At best you may be able to do something like a binary search, trying to create a container with some size and catching std::bad_alloc: most likely your "application error" and uncaught exception of that type.

这篇关于向量初始化期间未知的应用程序失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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