afrid18
afrid18

Reputation: 13

Default size of an int variable, when an object is created?

class Vect {
    public:
        Vect(int n);
        ~Vect();
        Vect(const Vect& original);
    private:
        int* data;
        int size;
};

Vect::Vect(int n) {
     size = n;
     data = new int[n];
}

Vect::~Vect() {
    delete [] data;
}

Vect::Vect(const Vect& original) {
    size = original.size;
    data = new int[size];

    for (int i = 0; i < size; ++i) {
        data[i] = original.data[i];
    }
}

#include <bits/stdc++.h>

using namespace std;

int main(void) {
    Vect a(100);
    Vect b = a;
    Vect c;
    c = a;
    return 0;
}

I have a Vect class now, in main I created a Vect object that variable c holds, what will be the default size of c.size ?? Or It won't have any default? If it does not have a default value then how does b have 100 (as in a.size is now equals b.size)?

Upvotes: 0

Views: 152

Answers (2)

francesco
francesco

Reputation: 7539

To answer your question: There is no default value of Vect::size unless you provide a default one in the class declaration, like

int size = 0;

If you do not initialize Vect::size you incur in undefined behavior.

Further, this code has a lot of problems, and in fact it does not compile.

Essential problems:

  • There is no constructor of Vect with no parameters, and there is not implicitly declared default constructor because you define Vect::Vect(int n). Hence the line Vect c; cannot compile.

  • The code c = a does not call your copy constructor Vect::Vect(const Vect& original), because the variable c already exists (assuming you have fixed the problem above). It does, instead, call the implicitly defined copy assignment operator, which you have not explicitly defined. This however just copies the member variables, thus it does not have the "right" logic since, after such a copy, both c.data and a.data point to the same area of memory. They are not a "copy", and so when a and c go out of scope, the allocated memory is freed twice (crashing the program). You need to define a correct copy-assignment operator Vect::operator=.

Other points:

Upvotes: 1

Fareanor
Fareanor

Reputation: 6805

What will be the default size of c.size ??

There is no default value except if you provide one. You should have a default constructor if you want to cover it (otherwise using uninitialized variable is undefined behaviour).

For example:

Vect::Vect() : data(nullptr), size(0)
{}

If it does not have a default value then how does b have 100 ?

Actually, when you write Vect b = a;, it is equivalent as Vect b(a);. In other words, the copy constructor is called (this is copy-initialization).

However, when you write:

Vect c;
c = a;

This is a copy-assignment. And you didn't provide one which is a problem because you need to handle the dynamic allocation of the data member (prevent self-assignment, deallocate if not nullptr and then reallocate with the new content), etc...
You should define the operator=() for the Vect class for your program to be well-formed.

Upvotes: 4

Related Questions