# Copy constructor with pointers

### Question

I have recently discovered that when I have pointers within a class, I need to specify a Copy constructor.

To learn that, I have made the following simple code. It compiles, but gives me runtime error when performing the copy constructor.

I am trying to copy just the value from the pointer of the copied object, but avoiding assigning the same address.

So, what's wrong here?

``````    class TRY{
public:
TRY();
~TRY();
TRY(TRY const &);

int *pointer;

void setPointer(int);
};

void TRY::setPointer(int a){
*pointer = a;

return;
}

TRY::TRY(){}

TRY::~TRY(){}

TRY::TRY(TRY const & copyTRY){
int a = *copyTRY.pointer;
*pointer = a;
}

int main(){

TRY a;
a.setPointer(5);

TRY b = a;

b.setPointer(8);

cout << "Address of object a = " << &a << endl;
cout << "Address of object b = " << &b << endl;

cout << "Address of a.pointer = " << a.pointer << endl;
cout << "Address of b.pointer = " << b.pointer << endl;

cout << "Value in a.pointer = " << *a.pointer << endl;
cout << "Value in b.pointer = " << *b.pointer << endl;

return 0;
}
``````

I'll be using this concept for other classes with lots of pointers in it, where I need to copy all values from on object to the other. Copying is initially necessary for this code, so I would like to keep the copying possibility (I won't be hiding the copy constructor as private).

Besides, the real class I need to implement has like 10 pointers, and it might be changing with time. Isn't there a somewhat smarter way to have a deep copy constructor in C++?...

1
15
9/4/2017 2:55:06 AM

With the statement `int* pointer` you have just defined a pointer but has not allocated any memory. First you should make it point to a proper memory location by allocating some memory like this: `int* pointer = new int`. Then in the copy constructor again you have to allocate the memory for the copied object. Also, don't forget to release the memory using delete in the destructor.

I hope this example helps:

``````class B
{

public:
B();
B(const B& b);
~B();
void setVal(int val);

private:
int* m_p;
};

B::B()
{
//Allocate the memory to hold an int
m_p = new int;

*m_p = 0;
}

B::B(const B& b)
{
//Allocate the memory first
m_p = new int;

//Then copy the value from the passed object
*m_p = *b.m_p;
}

B::~B()
{

//Release the memory allocated
delete m_p;
m_p = NULL;
}

void B::setVal(int val)
{
*m_p = val;
}
``````
18
4/23/2009 2:13:00 PM

I have recently discovered that when I have pointers within a class, I need to specify a Copy constructor.

It is not completely true. When you have pointers in your class and allocate the memory using `new` then you have to worry about copy constructor. Also, don't forget the assignment operator and destructor. You have to delete the memory allocated using `delete`.

It's called Law Of The Big Three.

Example:

``````  ~Matrix();  //Destructor
Matrix(const Matrix& m); //Copy constructor
Matrix& operator= (const Matrix& m); //Assignment operator
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.8692041,"math_prob":0.86165094,"size":2813,"snap":"2020-45-2020-50","text_gpt3_token_len":702,"char_repetition_ratio":0.1473834,"word_repetition_ratio":0.084337346,"special_character_ratio":0.28617135,"punctuation_ratio":0.18134715,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97665536,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-12-05T07:49:49Z\",\"WARC-Record-ID\":\"<urn:uuid:0e096aba-72d6-4e17-991f-889d3d90407e>\",\"Content-Length\":\"18251\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e3da43ae-9a09-4673-a682-adebce2af6d3>\",\"WARC-Concurrent-To\":\"<urn:uuid:cf5dfb0f-2ecd-4fe6-975d-a7606e51177e>\",\"WARC-IP-Address\":\"40.83.160.29\",\"WARC-Target-URI\":\"https://cpluspluspedia.com/en/knowledge-base/781760/copy-constructor-with-pointers\",\"WARC-Payload-Digest\":\"sha1:VK7QRJSGWBL7BLJP3CW7YSXOT6HML5FH\",\"WARC-Block-Digest\":\"sha1:YKOD75MIBDDRCEVNJGTYIZAFZVSTSSL4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141747323.98_warc_CC-MAIN-20201205074417-20201205104417-00043.warc.gz\"}"}