R
Richard Thompson
I've got a memory overwrite problem, and it looks as if a vector has
been moved, even though I haven't inserted or deleted any elements in
it. Is this possible? In other words, are there any circumstances in
which the STL will move a vector, or invalidate iterators to elements
in the vector, if you don't insert or remove elements?
My actual problem seems to be as follows:
I have class X, which contains an STL vector. The constructor does
very little, and leaves the vector empty. X has no assignment or copy
ctors:
class X {
...
vector<wibble> wvec;
};
I also have a heap of X's, which is maintained as a vector. The first
item on the heap is created by pushing an X onto the back of the empty
heap:
vector<wibble>& create_heap_item() {
...
heap.push_back(X(...));
return heap.back().wvec;
}
The routine which calls 'create_heap_item' gets a reference to wvec,
and uses it to insert items into wvec:
vector<wibble>& wref = create_heap_item();
wref.insert(wreg.begin(), a_wibble);
...
This all seems to work Ok as long as there's only one item on the
heap. However, when I add a second item, everything goes wrong. I have
various reverse iterators into the first wvec stored in other classes,
but after adding the second heap item these now appear to point into
the *second* wvec. It's almost as if the first wvec has moved. Or am I
just messing up my references?
Any ideas? This is driving me crazy...
TIA
Richard
been moved, even though I haven't inserted or deleted any elements in
it. Is this possible? In other words, are there any circumstances in
which the STL will move a vector, or invalidate iterators to elements
in the vector, if you don't insert or remove elements?
My actual problem seems to be as follows:
I have class X, which contains an STL vector. The constructor does
very little, and leaves the vector empty. X has no assignment or copy
ctors:
class X {
...
vector<wibble> wvec;
};
I also have a heap of X's, which is maintained as a vector. The first
item on the heap is created by pushing an X onto the back of the empty
heap:
vector<wibble>& create_heap_item() {
...
heap.push_back(X(...));
return heap.back().wvec;
}
The routine which calls 'create_heap_item' gets a reference to wvec,
and uses it to insert items into wvec:
vector<wibble>& wref = create_heap_item();
wref.insert(wreg.begin(), a_wibble);
...
This all seems to work Ok as long as there's only one item on the
heap. However, when I add a second item, everything goes wrong. I have
various reverse iterators into the first wvec stored in other classes,
but after adding the second heap item these now appear to point into
the *second* wvec. It's almost as if the first wvec has moved. Or am I
just messing up my references?
Any ideas? This is driving me crazy...
TIA
Richard