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

  • Home
  • SEARCH
  • 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 6224739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:43:21+00:00 2026-05-24T08:43:21+00:00

I have some java experience and am a beginner on C++. below is my

  • 0

I have some java experience and am a beginner on C++.

below is my code, its output is:

0 1 2 3 4 5 6 7 8 9
destructor ---s1
8791616 8785704 2
destructor ---s1

I expected the following output:

0 1 2 3 4 5 6 7 8 9
destructor ---abc
0 1 2
destructor ---s1

I can’t understand why the destructor releases the first object’s resource.
How can I print the output I expected?

#include <iostream>
using namespace std;
class Sequence{
    public:
        Sequence(int count=10,string name = "abc");
        void show();
        ~Sequence();

        int* _content;
        int _count;
        string _name;

};

Sequence::Sequence(int count,string name){
    _count = count;
    _content=new int[count];
    _name = name;
    for(int i=0;i<count;i++){
        _content[i]=i;
    }
}

Sequence::~Sequence(){
    cout << "destructor ---"<<_name<<endl;
    delete [] _content;
}

void Sequence::show(){
    for(int i=0;i<_count;i++)
        cout<<_content[i]<<" ";
    cout<<endl;
}

int main(){
    Sequence s1 = Sequence();
    s1.show();
    s1 = Sequence(3,"s1");
    s1.show();
}
  • 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-24T08:43:21+00:00Added an answer on May 24, 2026 at 8:43 am

    If you increase the warning level on your compiler, you’ll get a hint that your class contains pointers but you’re not defining Sequence(const Sequence&) or operator=(const Sequence&) (see What is The Rule of Three?).

    Because you don’t provide the copy constructor or assignment operator, the compiler provides these for you, which perform member-wise assignment.

    When you call s1 = Sequence(3,"s1");, you are doing the following (this may be unexpected to a Java developer):

    • Creating a new, temporary, Sequence of three with “s1” as its name
    • Assigning this to s1, which:
      • sets si._content to be the a pointer to the new array of three ints just created, leaking the old one of 10.
      • sets si._count to 3
      • sets si._name to "s1"
    • The temporary (and not s1) is then destroyed (in your actual output above, you see “s1” being destroyed twice), leaving _content pointing to free’d memory (which is why you see garbage on the second call to s1.show()).

    If you declare an assignment operator like this, you’ll get something closer to your expected output:

    Sequence& operator =(const Sequence& rhs)
    {
        if (this != &rhs)
        {
            delete [] _content;
    
            _count = rhs._count;
            _content = new int[_count];
            _name = rhs._name + " (copy)";
            for (int i = 0; i < _count ; ++i)
            {
                _content[i] = rhs._content[i];
            }
        }
        return *this;
    }
    

    You won’t, however, see:

    destructor ---abc
    

    …because you don’t destroy s1 while its _name contains "abc".

    s1 is destroyed when it goes out of scope at the closing }, which is why you see the second destructor call. With your code, this calls delete[] on s1._content a second time (it was deleted under the temporary, you’ll recall). This is likely to result in a crash right at the end of your program.

    I added " (copy)" to _name in my assignment operator to help to illustrate what is happening here.

    Please also take a look at What is the copy-and-swap idiom?, which is a very neat way to deal with classes with raw pointers. This will also generate the output you desire as the instance of s1 with _name of "abc" gets swapped out and destroyed. I’ve implemented this here, along with a few other little improvements so that you can see it working.

    N.B: The canonical way of creating an instance of a class is:

    Sequence s1; // Default constructor. Do not use parentheses [http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.2]!
    Sequence s2(3, "s2") // Constructor with parameters
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner at C++ but I have some experience using Java. I
I have some Java code that uses curly braces in two ways // Curly
Let's say I have some Java code: public class SomeClass { static { private
I have ported some LGPL code from Java to C#, which I plan to
I have to ship some groovy code to some users that have only java
I have a standalone Java app that has some licensing code that I want
Hi I'm a PHP developer and I have some experience with Java. I'm trying
I'm pretty new to Android development, but I have some experience with Java and
Today I started work on a small Java app. I have some experience with
I have some Java code that takes an XML (SOAP) message and returns the

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.