0

copy of vector must allocate new memory only on attempt to modify it.

#include <vector>
#include <iostream>
using namespace std;

int main()
{
   vector<char> vec(100 * 1000 * 1024, 3);  // allocating  100*1000*kib,  nice.
   vector<char> array[] = {vec,vec,vec,vec};  // now we using 500*1000*kib, VERY NICE, no more!

   return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
biv
  • 1,613
  • 3
  • 13
  • 21

1 Answers1

1

Copy on Write is not supported by the stl. Strings which did have COW, post C++11 no longer do. COW although attractive as a way to save memory means iterators become useless.

We will need to lock the underlying store on any access. This will need to be a slow atomic check in the event a copy of a container is passed to another thread.

doron
  • 27,972
  • 12
  • 65
  • 103