Hi everyone,
I'm sure I'm doing something silly here!

Basically my constructor creates a pointer initialised to null, then in one of my functions I create an integer array on the heap using the new keyword.

Now basically I have a data structure that needs to be copied, like so

mydatastructure structuretwo = structureone;

Now in my copy constructor I have got the basics...

mydatastructure::mydatastructure(const mydatastructure& copystructure)
{
	_numberData = new int [1000]; //Create a new array on the heap
	_numberData = copystructure._numberData;
}

Hope that makes sense as I've cut out most of the code. Now I know that I'm creating a new array the size of 1000 on the heap, but instead of the previous data structure been copied into the new one, the pointer ends up pointing to the memory location of the old datastructure instead.. where as I need it to be pointing at the new location and the old data to be copied to the new location too.

Can anybody help? I feel so close to the solution but I just can't get there!

Many Thanks :-)

Recommended Answers

All 5 Replies

What you really want to do is to copy the contents of the array:

mydatastructure::mydatastructure(const mydatastructure& copystructure)
{
	_numberData = new int [1000]; //Create a new array on the heap
	for (int i = 0; i < 1000; i++)
		_numberData[i] = copystructure._numberData[i];
}

Good luck!
Emil Olofsson

commented: Perfect answer. Just what I was looking for. +0

Perfect. I knew it would be something silly like that.

So why if I just do

_numberData = copystructure._numberData;

it changes the pointer... yet when I do

_numberData[0] = copystructure.numberData[0];

it only modifies that element? Would it not be more efficient doing it the way I originally wrote it? (and failed... haha)

Thanks again :-)

_numberData = copystructure._numberData;

Copying the pointer only gives you the illusion of the array being copied. But since both your structures member _numberData point at the same array, changing a value will have effect on all your instances of mydatastructure.

_numberData[0] = copystructure.numberData[0];

Yes, this will only copy the first element of the array. However, leaving the rest of the new array uninitialized will make what appears to be random values appear at the uninitialized indexes of the array :)

You should also be aware that your class will require a copy assignment operator and also a destructor to properly handle your dynamic array. Do a search for the 'rule of three' for details on this. Also do a search of Marshal Clines C++ faq lite (google for it) for details on constructors, destructors anc assignment operators.

http://www.parashift.com/c++-faq-lite/index.html

Copying the pointer only gives you the illusion of the array being copied. But since both your structures member _numberData point at the same array, changing a value will have effect on all your instances of mydatastructure.


Yes, this will only copy the first element of the array. However, leaving the rest of the new array uninitialized will make what appears to be random values appear at the uninitialized indexes of the array :)

I understand that.. but you'd have thought it would make sense to do it all in one line. Anyway it works and that's the main thing :)

You should also be aware that your class will require a copy assignment operator and also a destructor to properly handle your dynamic array. Do a search for the 'rule of three' for details on this. Also do a search of Marshal Clines C++ faq lite (google for it) for details on constructors, destructors anc assignment operators.

http://www.parashift.com/c++-faq-lite/index.html

I've made a destructor to handle it. I didn't realise I'd need an assignment operator as well though, but I'll look into this.

Thanks again :)

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.