diff --git a/applications/test/DynamicList/DynamicListTest.C b/applications/test/DynamicList/DynamicListTest.C index a077f2d9d7..6cdbb84890 100644 --- a/applications/test/DynamicList/DynamicListTest.C +++ b/applications/test/DynamicList/DynamicListTest.C @@ -53,7 +53,7 @@ int main(int argc, char *argv[]) forAll(ldl, i) { Info<< " " << ldl[i].size() << "/" << ldl[i].allocSize(); - } + } Info<< endl; List > ll(2); @@ -64,11 +64,38 @@ int main(int argc, char *argv[]) forAll(ldl, i) { Info<< " " << ldl[i].size() << "/" << ldl[i].allocSize(); - } + } Info<< endl; Info<< "" << ll << "" << nl << endl; + + // test the transfer between DynamicLists + DynamicList dlA; + DynamicList dlB; + + for (label i = 0; i < 5; i++) + { + dlA.append(i); + } + dlA.setSize(10); + + Info<< "" << dlA << "" << nl << "sizes: " + << " " << dlA.size() << "/" << dlA.allocSize() << endl; + + dlB.transfer(dlA); + + // provokes memory error if previous transfer did not maintain + // the correct allocated space + dlB[6] = 6; + + Info<< "Transferred to dlB" << endl; + Info<< "" << dlA << "" << nl << "sizes: " + << " " << dlA.size() << "/" << dlA.allocSize() << endl; + Info<< "" << dlB << "" << nl << "sizes: " + << " " << dlB.size() << "/" << dlB.allocSize() << endl; + + return 0; } diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index 8bb2a2561c..1a40bdd0ac 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -128,7 +128,7 @@ inline void Foam::DynamicList::transfer(List& lst) { allocSize_ = lst.size(); - List::transfer(lst); // take over storage, null lst + List::transfer(lst); // take over storage, clear addressing for lst. } @@ -139,8 +139,11 @@ Foam::DynamicList::transfer DynamicList& lst ) { - allocSize_ = lst.allocSize(); - List::transfer(lst); // take over storage, null lst. + // take over storage as-is (without shrink), clear addressing for lst. + allocSize_ = lst.allocSize_; + lst.allocSize_ = 0; + + List::transfer(static_cast&>(lst)); } @@ -230,10 +233,13 @@ inline void Foam::DynamicList::operator= template inline void Foam::DynamicList::operator= ( - const List& l + const List& lst ) { - List::operator=(l); + // make the entire storage available for the copy operation: + List::size(allocSize_); + + List::operator=(lst); allocSize_ = List::size(); }