std::vector<T> constructor does not throw when asked for an erroneous initialization size
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
gcc-defaults (Ubuntu) |
New
|
Undecided
|
Unassigned |
Bug Description
When asked to allocate memory for a very large vector (memory chuncks larger than 2^31), the constructor of std::vector<T> does not throw a bad_alloc exception and the built vector reports an erroneous size. A simple testcase reproduces the bug on a 32-bit architecture:
#include<vector>
#include<iostream>
#include<limits>
int main(){
std::
std::cout << "vector<double> max size = " << v.max_size() << std::endl;
std::cout << "std::numeric_
// sizeof(double) = 8
// 2^31 / 8 - 1 = 268435455
std::
std::cout << "requested size = 268435455, vec size = " << vec.size() << std::endl;
std::
// 2^31 / 8 = 268435456
std::
std::cout << "requested size = 268435456, vec size = " << vec2.size() << std::endl;
std::
// for the example
std::
std::cout << "requested size = 300000000, vec size = " << vec3.size() << std::endl;
std::
}
Similar values are obtained for capacity() also.
Note that bad_alloc exceptions are being thrown for even larger requested sizes (500 000 000 for instance) but it seems they should also be thrown starting at sizes corresponding to 2^31.
I'm running Lucid Lynx (10.04) on a MacBookPro:
Core2Duo (2.53GHz), 3.7 Gb RAM
kernel 2.6.32-
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
ProblemType: Bug
DistroRelease: Ubuntu 10.04
Package: gcc 4:4.4.3-1ubuntu1
ProcVersionSign
Uname: Linux 2.6.32-
NonfreeKernelMo
Architecture: i386
Date: Fri Sep 10 12:57:52 2010
InstallationMedia: Ubuntu 10.04 LTS "Lucid Lynx" - Release i386 (20100429)
ProcEnviron:
LANG=fr_BE.utf8
SHELL=/bin/bash
SourcePackage: gcc-defaults