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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:14:48+00:00 2026-05-25T20:14:48+00:00

I hava a class class vlarray { public: double *p; int size; vlarray(int n)

  • 0

I hava a class

class vlarray {
public:
    double *p;
    int size;

    vlarray(int n) {
        p = new double[n];
        size = n;
        for(int i = 0; i < n; i++)
            p[i] = 0.01*i;
    }

    ~vlarray() {
        cout << "destruction" << endl;
        delete [] p;
        size = 0;
    }
};

when I use in main

int main() {
    vlarray a(3);
    {
        vlarray b(3);
        b.p[0] = 10;
        for(int i = 0; i < 3; i++) {
            cout << *(b.p+i) << endl;
        }
        a = b;
    }    // the magic happens here deallocation of b
    for(int i = 0; i < 3; i++) {
        cout << *(a.p+i) << endl;
    return 0;
}

when b deallocated smth happens to a .. what is the problem, why that problem occurs and how to avoid such type of problems?

  • 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-25T20:14:49+00:00Added an answer on May 25, 2026 at 8:14 pm

    There seems to be some confusion amongst the existing answers on this question, so I am going to jump in.


    Immediate issue

    Your primary issue is that you have not defined your own copy assignment operator. Instead, the compiler generates a naive one for you, so that when you run a = b, the pointer inside b is copied into a. Then, when b dies and its destructor runs, the pointer now inside a is no longer valid. In addition, a‘s original pointer has been leaked.

    Your own copy assignment operator will need to delete the existing array, allocate a new one and copy over the contents from the object you’re copying..

    More generally

    Going further, you will also need to define a few other things. This requirement is neatly summed-up as the Rule of Three (C++03) or Rule of Five (C++11), and there are plenty of explanations online and in your favourite, peer-recommended C++ book that will teach you how to go about satisfying it.

    Or, instead…

    Better still, you could start using an std::vector instead of manually allocating everything, and avoid this entire mess:

    struct vlarray {
        std::vector<double> p;
    
        vlarray(int n) {
            p.resize(n);
            for(int i = 0; i < n; i++)
                p[i] = 0.01*i;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hava some code like this: class LooperThread extends Thread { public Handler mHandler;
i hava a class that hold a list for each request but each request
I hava a main view, where you have one object and you can change
I hava a supeclass called Car with 3 subclasses. class Ford extends Car{ }
I have a Player class that extends CCNode, and i want to hava a
I hav a base class which is abstract. I am inheritting a new class
Hi I'm new to c language i hava a problem : i want to
I hava a jQuery UI datepicker which I intend to use with a textbox
i have some small problem. I hava some class Processor and Process . Process
I hava a url such as search.do?offset=20 offset sometimes is in the url sometimes

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.