------- 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