This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/37818] New: std::vector copy construction of empty vector causes malloc of size 1
- From: "sweeboonlim at yahoo dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 13 Oct 2008 17:38:48 -0000
- Subject: [Bug c++/37818] New: std::vector copy construction of empty vector causes malloc of size 1
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
When copying empty vector with copy constructor, there should be no memory
allocation instead of a malloc of size 1. This is due to allocate(0) calls
operator new(0) which calls malloc(1).
Here is a sample program that will demonstrate the memory allocation of size 1.
#include <vector>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[])
{
vector<int> a;
vector<int> b(a); // <-- this causes malloc of size 1.
printf("&a[0] %zx\n", size_t(&a[0]));
printf("&b[0] %zx\n", size_t(&b[0]));
return 0;
}
A fix that resolves this problem is as follows:
In stl_vector.h:
_Vector_base(const allocator_type& __a)
: _M_impl(__a)
{ }
_Vector_base(size_t __n, const allocator_type& __a)
: _M_impl(__a)
{
>>> if (__n == 0) return; <<< // only allocate if size != 0
this->_M_impl._M_start = this->_M_allocate(__n);
this->_M_impl._M_finish = this->_M_impl._M_start;
this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
}
Additional information below:
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ../configure -prefix=/packages/gcc-4.2.3/ --disable-bootstrap
--enable-languages=c,c++ : (reconfigured) ../configure
-prefix=/packages/gcc-4.2.3/ --disable-bootstrap --enable-languages=c,c++
--disable-libmudflap
Thread model: posix
gcc version 4.2.3
/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../libexec/gcc/x86_64-unknown-linux-gnu/4.2.3/cc1plus
-E -quiet -v -iprefix
/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3/
-D_GNU_SOURCE testvectorbug.cc -mtune=generic -O3 -fpch-preprocess -o
testvectorbug.ii
ignoring nonexistent directory
"/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3/../../../../x86_64-unknown-linux-gnu/include"
ignoring nonexistent directory "/packages/gcc-4.2.3//include/c++/4.2.3"
ignoring nonexistent directory
"/packages/gcc-4.2.3//include/c++/4.2.3/x86_64-unknown-linux-gnu"
ignoring nonexistent directory
"/packages/gcc-4.2.3//include/c++/4.2.3/backward"
ignoring nonexistent directory "/packages/gcc-4.2.3//include"
ignoring nonexistent directory
"/packages/gcc-4.2.3//lib/gcc/x86_64-unknown-linux-gnu/4.2.3/include"
ignoring nonexistent directory
"/packages/gcc-4.2.3//x86_64-unknown-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3/../../../../include/c++/4.2.3
/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3/../../../../include/c++/4.2.3/x86_64-unknown-linux-gnu
/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3/../../../../include/c++/4.2.3/backward
/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3/include
/usr/local/include
/usr/include
End of search list.
/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../libexec/gcc/x86_64-unknown-linux-gnu/4.2.3/cc1plus
-fpreprocessed testvectorbug.ii -quiet -dumpbase testvectorbug.cc
-mtune=generic -auxbase testvectorbug -O3 -version -o testvectorbug.s
GNU C++ version 4.2.3 (x86_64-unknown-linux-gnu)
compiled by GNU C version 3.4.4 20050721 (Red Hat 3.4.4-2).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: f61b91c200f26e3d9a5a8b13539acbc3
as -V -Qy -o testvectorbug.o testvectorbug.s
GNU assembler version 2.15.92.0.2 (x86_64-redhat-linux) using BFD version
2.15.92.0.2 20040927
/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../libexec/gcc/x86_64-unknown-linux-gnu/4.2.3/collect2
--eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o
testvectorbug /usr/lib/../lib64/crt1.o /usr/lib/../lib64/crti.o
/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3/crtbegin.o
-L/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3
-L/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc
-L/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3/../../../../lib64
-L/lib/../lib64 -L/usr/lib/../lib64
-L/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3/../../..
testvectorbug.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/nfs/ystools/vol/ystools/releng/build/Linux_2.6_rh4_x86_64/tools/gcc/4.2.3/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.2.3/crtend.o
/usr/lib/../lib64/crtn.o
--
Summary: std::vector copy construction of empty vector causes
malloc of size 1
Product: gcc
Version: 4.2.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: sweeboonlim at yahoo dot com
GCC target triplet: x86_64-unknown-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37818