```
#include <vector>
typedef std::vector<char> vc;
typedef std::vector<vc> vvc;

vvc f() {
  const int N = (1 << 15);
  return vvc(N, vc(N)); // 1 GB
}

int main () {
  vvc v; 
  v = f();
  while(true);  // Why 2GB allocated ?
  return 0;
}

```

Compiled with both clang -O2 and g++ -O2. Same behavior.

**Edit:** There are multiple ways to fix this code pointed in some of the answers.
But my point is to understand this code. Of course there is a temporary object, but it should disappear at the semicolon and 1GB of memory should be returned to the system. The question intends to ask why it does not happen.

**Edit 2:** The destructor of temporary object is indeed called before semicolon.