Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7066679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:06:36+00:00 2026-05-28T05:06:36+00:00

While trying to experiment with copy construction by ‘pass by value’ and the followed

  • 0

While trying to experiment with copy construction by ‘pass by value’ and the followed destruction, I tried this code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Rock{
int sz;
public:
Rock():sz(0){cout<< "Default ctor"<<endl;}
~Rock(){cout<< "Dtor"<<endl;}
Rock(const Rock& r){ cout << "Copy ctor" << endl; sz = r.sz;}
Rock& operator=(const Rock& r) {cout << "In assignment op" << endl; sz = r.sz;}
};

int main()
{
vector<Rock> rocks;
Rock a, b, c;
rocks.push_back(a);
rocks.push_back(b);
rocks.push_back(c);
return 0;
}

And got the below output. Up to line 7 everything is fine, however I could not understand what happens from then on. Can someone clarify?

Default ctor
Default ctor
Default ctor
Copy ctor
Copy ctor
Copy ctor // all fine I got it...
Dtor
Copy ctor
Copy ctor
Copy ctor
Dtor
Dtor
Dtor
Dtor
Dtor
Dtor
Dtor
Dtor
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:06:36+00:00Added an answer on May 28, 2026 at 5:06 am

    Let’s associate the output to the corresponding code lines.

    Rock a, b, c;
    
    Default ctor
    Default ctor
    Default ctor
    

    That one you probably figured out yourself. 🙂

    rocks.push_back(a);
    
    Copy ctor
    

    Again, you probably figured that out correctly.

    rocks.push_back(b);
    
    Copy ctor
    Copy ctor // all fine I got it...
    Dtor
    

    Your comment is clearly wrong, because you almost certainly did not associate both constructor calls with this statement 🙂

    What happens is that when adding a copy of a, the vector only allocated enough memory to store that one copy of a (it would have been allowed to allocate more, though). Therefore it has to allocate a new memory block large enough to hold both copies of a and b, copy the copy of a it stored in the old memory block over to the new one,copy b after it, and then destroy the original copy of a before deallocating the (now no longer needed) original memory block.

    rocks.push_back(c);
    
    Copy ctor
    Copy ctor
    Copy ctor
    Dtor
    Dtor
    

    From the above explanation, you now should be able to guess what happens here.

    Note however that if you pushed back yet another element to the vector, it’s quite likely that you’d again get just one copy constructor and no destructor call. That’s because a typical strategy for vectors is to double the allocated memory in each step, thus when pushing back c it most probably allocated space for 4 objects. Actually std::vector is required to use an exponential strategy (it is not required to use the factor 2, though, and it has been argued that the golden mean is a much better factor).

    }
    
    Dtor
    Dtor
    Dtor
    Dtor
    Dtor
    Dtor
    

    Here the three objects c, b, a, and then the three objects in the vector are destructed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to experiment with software defined radio concepts. From this article I've tried
I'm trying to compile this code with GCC 4.5.0: #include <algorithm> #include <vector> template
While trying to generate classes from a xsd, i got this error: java.lang.IllegalArgumentException: Illegal
I'm trying to experiment with malloc and free in assembly code (NASM, 64 bit).
While trying out an experimental UINavigationController-based iPhone application, I ran into a problem when
While trying to answer a question in the vicinity ' Unit Testing WPF Bindings
While trying to use LINQ to SQL I encountered several problems. I have table
While trying to connect to a database that's set to read only, the connection
While trying to integrate Yahoo Media Player into my own website, I want to
While trying to make one of my python applications a bit more robust in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.