Re: Under what circumstances can the STL move a vector?

From: Richard Thompson (
Date: 02/06/05

Date: Sun, 06 Feb 2005 17:07:40 +0000

[posted after my previous reply to myself, which hasn't turned up yet]

After re-reading the first set of replies, and looking at my test code
(just posted), it seems to me that some of you have already answered
the question. When I push a new element onto the back of the heap (the
second call to 'create_heap_item') then the heap is presumably
re-allocated, because it's size has just doubled. In the process, the
vector in the first element on the heap is moved, thus invalidating
l1rbegin and l1rend.

I had assumed, somewhere at the back of my mind, that the 'control
structure' (?) for the first vector on the heap would move, but that
the 'real' vector, which is presumably stored somewhere else on the
system heap, wouldn't move. Presumably this is wrong? Are the vector
elements actually stored in struct X, and moved with struct X?

It seems to me that if you're writing library code (as I am), and you
don't know how your classes are going to be used, then you can *never*
rely on your iterators remaining valid? Doesn't this severely restrict
the use of the STL?


Relevant Pages

  • Re: Two questions about C# datatypes
    ... int is value type so it is stored on stack. ... class is reference type ... and it is stored on heap. ... a class containing a struct will create an object where the struct is part of the memory allocated to the object. ...
  • Re: How to Get Parent Object in case of compostion
    ... Public Class B ... A struct may either be created "in" a class instance on the heap, ... then you could use __offsetof to get to the parent pointer. ...
  • Re: Declaring array of references.
    ... generator, where speed is the only criterion. ... values will also be on the heap. ... I also assume that references are the size of one int, ... heap and the contents of the struct copied into it. ...
  • Re: General Question About Structs and Stack
    ... > A struct is stored on the stack and a class on the heap. ... > A struct is a value type while a class is a reference type. ...
  • RE: General Question About Structs and Stack
    ... > loaded on the heap, after creating an instance of the struct and populating ... >> which would mean in the current stack frame would exist a pointer to a memory ... >> stack frame to hold its location. ...