std::vector<T> constructor does not throw when asked for an erroneous initialization size

Bug #634826 reported by Emmanuel Rachelson
6
This bug affects 1 person
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::vector<double> v;
  std::cout << "vector<double> max size = " << v.max_size() << std::endl;
  std::cout << "std::numeric_limits<size_t>::max() = " << std::numeric_limits<size_t>::max() << std::endl;
  // sizeof(double) = 8
  // 2^31 / 8 - 1 = 268435455
  std::vector<double> vec(268435455, 8.4);
  std::cout << "requested size = 268435455, vec size = " << vec.size() << std::endl;
  std::vector<double>().swap(vec);
  // 2^31 / 8 = 268435456
  std::vector<double> vec2(268435456, 8.4);
  std::cout << "requested size = 268435456, vec size = " << vec2.size() << std::endl;
  std::vector<double>().swap(vec2);
  // for the example
  std::vector<double> vec3(300000000, 8.4);
  std::cout << "requested size = 300000000, vec size = " << vec3.size() << std::endl;
  std::vector<double>().swap(vec3);
}

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-24-generic-pae
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3

ProblemType: Bug
DistroRelease: Ubuntu 10.04
Package: gcc 4:4.4.3-1ubuntu1
ProcVersionSignature: Ubuntu 2.6.32-24.42-generic-pae 2.6.32.15+drm33.5
Uname: Linux 2.6.32-24-generic-pae i686
NonfreeKernelModules: nvidia wl
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

Revision history for this message
Emmanuel Rachelson (emmanuel-rachelson) wrote :
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.