Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::copy for dynamically allocated pointers

This is probably a very newbie question. The class I'm working on (let's call it MyClass) contains the following variables

 int nbins;
 double * bins; // = new double[nbins+i]

Since I'm implementing a copy constructor

 MyClass( const MyClass& source )

I need to copy source.bins. I was going to use std::copy from algorithm, but all the examples I've found show how to copy static array like double bins[] = ... or standard data structure like std::vector. I can't find any example in which std::copy is used on pointers like double * bins = new double[nbins+i]. So, my question is: is legal to use std::copy on pointers, like for example

double * bins = new double[nbins+1]; //is this necessary?
std::copy( std::begin( source.bins ), std::end(source.bins), std::begin(bins) );

or should I rather rely on the C memcpy

double * bins = new double[nbins+1]; //again, is this necessary?
memcpy( source.bins, bins, (nbins+1) /* *sizeof(double) ?*/ );

So, my questions are basically two:

1) Does std::end(bins) know where is the end of bins (=new double[nbins+1])?

2) Do I need to allocate the memory for the destination before doing the copy? Or the copy (or memcpy) takes care of that?

like image 396
Blackphoenix Avatar asked Oct 29 '13 16:10

Blackphoenix


People also ask

Does std :: copy allocate?

Does std :: copy allocate? Module std::clone These types do not require allocation to copy and do not have finalizers (i.e., they do not contain owned boxes or implement Drop ), so the compiler considers them cheap and safe to copy.

Which operator is used to dynamically allocate memory?

To dynamically allocate memory in C++, we use the new operator. De-allocation: Deallocation is the "clean-up" of space being used for variables or other data storage. Compile time variables are automatically deallocated based on their known extent (this is the same as scope for "automatic" variables)

How do you allocate a pointer in C++?

Syntax to use new operator: To allocate memory of any data type, the syntax is: pointer-variable = new data-type; Here, pointer-variable is the pointer of type data-type. Data-type could be any built-in data type including array or any user defined data types including structure and class.

How do you declare a dynamic array in C++?

Dynamic arrays in C++ are declared using the new keyword. We use square brackets to specify the number of items to be stored in the dynamic array. Once done with the array, we can free up the memory using the delete operator. Use the delete operator with [] to free the memory of all array elements.


1 Answers

1) Does std::end(bins) know where is the end of bins (=new double[nbins+1])?

No, std::begin and std::end require that the array be of fixed size.

2) Do I need to allocate the memory for the destination before doing the copy? Or the copy (or memcpy) takes care of that?

You need to allocate the memory. Both copy and memcpy assume that you provide them with a block of memory that can be written to.

So yes, you can use std::copy, but without std::begin and std::end:

double * bins = new double[nbins+1]; //is this necessary?
std::copy( source.bins , source.bins + N, bins );

where N is the length or the array pointed at by source.bins. Obviously you have to make sure you are not accessing either array out of bounds.

like image 95
juanchopanza Avatar answered Sep 23 '22 01:09

juanchopanza