Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating an array from an existing array

32 views
Skip to first unread message

Doug Mika

unread,
Jun 4, 2015, 2:57:34 PM6/4/15
to
Hi

if I have an existing array of simple data types, and I use std::copy to create a copy of the array, the simple data types are simply copied into my second array? True/False?

if I have an existing array of my own user defined objects, and I use std::copy to create a copy of the array, each element of the array is copied using my objects copy constructor? True/False?

if I have an existing array of pointers, and I use the std::copy function to create a copy of the array, is each pointer going to be merely copied, or is the object (to which the pointer points to) going to have it's copy constructor invoked?

Thanks
Doug

Paavo Helde

unread,
Jun 4, 2015, 3:29:25 PM6/4/15
to
Doug Mika <doug...@gmail.com> wrote in
news:76254bc0-615c-40f4...@googlegroups.com:

> Hi
>
> if I have an existing array of simple data types, and I use std::copy
> to create a copy of the array, the simple data types are simply copied
> into my second array? True/False?

You cannot create an array by std::copy. std::copy merely copies elements
from one place to another, so the destination array must be already
exisiting. Yes, it can grow a container like std::vector by using
std::back_inserter, but this won't work with a C or C++ array (which has
fixed size), and the destination container must still be created
beforehand.

But otherwise, you are right, std::copy indeed copies data (doh).

>
> if I have an existing array of my own user defined objects, and I use
> std::copy to create a copy of the array, each element of the array is
> copied using my objects copy constructor? True/False?

I guess it's more likely that the assignment operator gets used, unless
you are using std::back_inserter or something like that.

> if I have an existing array of pointers, and I use the std::copy
> function to create a copy of the array, is each pointer going to be
> merely copied, or is the object (to which the pointer points to) going
> to have it's copy constructor invoked?

Raw pointers belong to "simple data types", so as per your first point
they just get copied. Nothing more happens. If you want deep copying this
has to be arranged specially.

hth
Paavo

Doug Mika

unread,
Jun 4, 2015, 4:30:56 PM6/4/15
to
So let's say I have an array of pointers to my user defined objects MyObjects:

int main(){
MyObject* myArray[3];
myArray[0]=new MyObject();
myArray[1]=new MyObject();
myArray[2]=new MyObject();

MyObject* mySecondArray[3];
mySecondArray[0]=myArray[0];
mySecondArray[1]=myArray[1];
mySecondArray[2]=myArray[2];
return 0;
}

will mySecondArray contain copies of the pointers in myArray (and hence point to the same objects as elements of myArray) (I hate asking this as all my logic seems to tell me yes, but for some reason I have doubts about this), and of course it makes no difference whether MyObject implements a copy assignment operator as we are dealing with pointers to MyObject and not MyObject instances, correct?

Victor Bazarov

unread,
Jun 4, 2015, 4:37:25 PM6/4/15
to
On 6/4/2015 4:30 PM, Doug Mika wrote:
>[..]
> So let's say I have an array of pointers to my user defined objects MyObjects:
>
> int main(){
> MyObject* myArray[3];
> myArray[0]=new MyObject();
> myArray[1]=new MyObject();
> myArray[2]=new MyObject();
>
> MyObject* mySecondArray[3];
> mySecondArray[0]=myArray[0];
> mySecondArray[1]=myArray[1];
> mySecondArray[2]=myArray[2];
> return 0;
> }
>
> will mySecondArray contain copies of the pointers in myArray (and
> hence point to the same objects as elements of myArray) (I hate
> asking this as all my logic seems to tell me yes, but for some reason
> I have doubts about this), and of course it makes no difference
> whether MyObject implements a copy assignment operator as we are
> dealing with pointers to MyObject and not MyObject instances,
> correct?

Correct.

You're assigning the values of elements of one array to elements of the
other array. Once this is done, the *values* of the 0th elements are
the same (and of the 1st ones, and the 2nd ones). What is the type of
those elements? They are pointers. If their values are the same, it
means they point to the same object. No matter how many pointers you
create, if their values are all the same, they all point to the same
object. The nature of the object is irrelevant. Of course, if the
value is 'null', they all point to nothing. Still it's the same
nothing. :-)

V
--
I do not respond to top-posted replies, please don't ask

Paavo Helde

unread,
Jun 4, 2015, 5:05:48 PM6/4/15
to
Doug Mika <doug...@gmail.com> wrote in
news:434f9bee-74d0-4295...@googlegroups.com:
Right. Maybe it will help you to notice that these assignments are
essentially C code (C has pointers, arrays of pointers and 'mySecondArray
[0]=myArray[0];' is valid C code assigning one pointer value to another).
Of course, C does not do anything behind the scenes or automatically, so
this assignment is just a copy of the pointer value and nothing else. And
as C++ strives to be mostly compatible with C, it cannot do anything
other with this code either (regardless of if it would make any sense or
not).

hth
Paavo
0 new messages