This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/32068] vector push_back()/resize() causes assignment to deleted memory
- From: "pcarlini at suse dot de" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 24 May 2007 13:01:29 -0000
- Subject: [Bug c++/32068] vector push_back()/resize() causes assignment to deleted memory
- References: <bug-32068-14512@http.gcc.gnu.org/bugzilla/>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Comment #1 from pcarlini at suse dot de 2007-05-24 14:01 -------
(In reply to comment #0)
> If I change the assignments to
>
> int x = build_recursive(n-1);
> int y = build_recursive(n-1);
> b[i].left = x;
> b[i].right = y;
>
> there is no segmentation fault anymore. It seems to me the original
> code has the b[i] value cached, hence, during the assignment to
> b[i].right, it uses old b[i] which may be invalidated after
> push_back() due to resize.
Right, the problem is that build_recursive does a push_back, which reallocates
the vector, and when it returns the original b[i] is not pointing to vector
memory anymore. This kind of code is clearly invalid, may work by chance on an
implementation which pre-allocates at start some capacity, but will fail at
some large size anyway.
--
pcarlini at suse dot de changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32068