Hello I was researching the copy constructor and found this exampleo a website:

#include <iostream>
 
class Array
{
  public:
    int size;
    int* data;
 
    Array(int size)
        : size(size), data(new int[size]) {}
 
    ~Array() 
    {
        delete[] data;
    }
};
 
int main()
{
    Array first(20);
    first.data[0] = 25;
 
    {
        Array copy = first;
 
        std::cout << first.data[0] << " " << copy.data[0] << std::endl;
 
    }    // (1)
 
    first.data[0] = 10;    // (2)
}

Is this code vulnerable because when it makes copy=first it is actually linking there adresses or is my understanding of a copy constructor's inner dynamics wrong?

Recommended Answers

All 8 Replies

That to me seems like a perfect example of what could go wrong if you don't define a copy ctor for classes which have pointers to memory on the heap. Shallow copy as you said will have the 'data' pointer of both the classes point to the same memory location. Once the local object gets deleted its dtor will delete that memory, 'data' is now a dangling pointer and any access to it can cause segmentation faults, unexpected output etc.

My questions following woll refer to this program shown later in the article:
box.h:

class box{
private:
int value;
public:
void def_v(int in) //define value
{this.value=in;}
void box(){value=0}; //normal constructor
void box(box c) //copyctor (copy constructor)
{
       this.value=c.value;
};
};

main:

#include<iostream>
#include"box.h"
using namespace std;
int main ()
{
int in=5;
box a;
a.def_v(5);
box b;
b=new box(a)
system("pause");
return 0;
}

So when I call a copy constructor do I need the new in object= new copyctor(a object) - which is assuming that that command is an equivalent to b object(copyctor(a object).

Also what is really going on behind the scenes in the copy constructor when explicitly defined? When the target of a
class copying is passed to a copyctor how does that relate
to the class object in the definition of the copyctor in the class
box?

This copy ctor is totally screwed

void box(box c) //copyctor (copy constructor)
{
       this.value=c.value;
};

you can't pass the object by value as it will create an infinite loop. Remember the 3 main instances when the copy ctor gets called?, 'pass by value' is one of them. copy ctor should always use pass by reference. 'const ref' will be best.

Nothing much is going on behind the scenes in the copy ctor, you just copy elements from one object to another.

This program is full of errors, I'm sure you will encounter them when you compile it.

Does the new need to be there in the box b= new box(c)?
When the program passes box(c) to the parameter box( box d){this.value=d.value}
What happens with objects c and d? Do there adresses become equal?

That's for you to decide whether you want to allocate memory on the heap or on stack. And new returns a pointer so it should be box* b;

So when you say to pass by reference do you mean to just pass box b= box(&a) or to pass b=box(a) to the copy constructor that is defined by box( box &b){..} in the class?

Also, what is the class object really in terms of data-type? is it really just a pointer and that is why it can be dynamically allocated or am I just really confused?

I don't know about you but I am definitely totally confused ;) !!!!

lol?....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.