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 5960823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:52:45+00:00 2026-05-22T18:52:45+00:00

I wrote the following code: #include <iostream> #include <vector> using namespace std; class AClass

  • 0

I wrote the following code:

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

class AClass
{
    public:
        int data;

        AClass() 
        { data = -333; cout << "+ Creating default " << data << endl; }

        AClass(const AClass &copy) 
        { data = copy.data; cout << "+ Creating copy of " << data << endl; }

        AClass(int d) 
        { data = d; cout << "+ Creating " << data << endl; }

        ~AClass() 
        { cout << "- Deleting " << data << endl; }

        AClass& operator = (const AClass &a)
        {  data = a.data; cout << "= Calling operator=" << endl; }
};

int main(void)
{
    vector<AClass> v;

    for (int i = 3; i--; )
        v.push_back(AClass(i));

    vector<AClass>::iterator it = v.begin();
    while (it != v.end())
        cout << it->data << endl, it++;

    return 0;
}

And the output from the program is:

+ Creating 2
+ Creating copy of 2
- Deleting 2
+ Creating 1
+ Creating copy of 1
+ Creating copy of 2
- Deleting 2
- Deleting 1
+ Creating 0
+ Creating copy of 0
+ Creating copy of 2
+ Creating copy of 1
- Deleting 2
- Deleting 1
- Deleting 0
2
1
0
- Deleting 2
- Deleting 1
- Deleting 0

Then I changed the class to:

class AClass
{
    public:
        int data;

        AClass(int d) 
        { data = d; cout << "+ Creating " << data << endl; }

        ~AClass() 
        { cout << "- Deleting " << data << endl; }
};

And the output becomes:

+ Creating 2
- Deleting 2
+ Creating 1
- Deleting 2
- Deleting 1
+ Creating 0
- Deleting 2
- Deleting 1
- Deleting 0
2
1
0
- Deleting 2
- Deleting 1
- Deleting 0

It appears that vector is making copies of existing objects when new ones are added, but it seems like a lot of unnecessary allocation/deletion is taking place. Why is this? Also, why does the second version work when I haven’t provided a copy constructor?

  • 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-22T18:52:46+00:00Added an answer on May 22, 2026 at 6:52 pm

    It appears that vector is making copies of existing objects when new ones are added

    When you add an element, e.g. with v.push_back(AClass(i));, what is done is a temporary AClass object is created and passed to push_back. push_back must then copy this object into the container.

    The other reason that you see copies made is that std::vector stores its elements contiguously in an array. If there is no room left in the underlying array and you try to add another element to the end, the std::vector must create a new array, copy the elements from the old array into a new one, and then insert the new element at the end. If you don’t want this to occur, you can call std::vector::reserve to reserve sufficient space in the std::vector before you start inserting elements, or you can use a different sequence container, like std::deque, which does not store its elements contiguously.

    it seems like a lot of unnecessary allocation/deletion is taking place

    In a C++ program, objects are frequently created and destroyed. Note that in your program, AClass is very cheap to copy: its size is probably four or eight bytes, just large enough to hold its int data member.

    If you have a type that is expensive to copy (e.g., perhaps you have a large tree data structure that has thousands of nodes), then yes, copying may be too expensive. In that case, you can store smart pointers to dynamically allocated objects in the std::vector instead (a std::vector<shared_ptr<AClass> >, for example). If your compiler supports rvalue references and has a move-aware Standard Library implementation, you can make an expensive-to-copy type movable by implementing a move constructor and move assignment operator and using emplace_back instead of push_back.

    why does the second version work when I haven’t provided a copy constructor?

    If you don’t declare a copy constructor, the compiler provides a default copy constructor for you.

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

Sidebar

Related Questions

i wrote the following code.... #include< iostream> #include< fstream> using namespace std; int main()
Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int
I'm using the following code to write data through a named pipe from one
Why does the following code not work #include <iostream> #include <fstream> #include <stdio.h> #include
I have the following piece of code in C++: #include <iostream> #include <fstream> #include
I wrote the following code to read a character array and print it. #include<stdio.h>
Lets say we have the following code: #include <iostream> #include <string> struct A {
i have following code for copying content of vector into file #include<iterator> #include<algorithm> #include<fstream>
I wrote the following code to read the content of a file: #include <ifstream>
In C++Builder, I wrote the following code (in Button1Click handler), When I run 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.