0

Just on the following code do I need to allocate memory with new for floatArray or will the copy function allocate memory for me?

Also is it okay to copy a constant vector to an array this way? Why?

vector<float> floatVector;
//filled floatVector here

float *floatArray = new float[floatVector.size()];
copy(floatVector.begin(),floatVector.end(),floatArray);          
delete[] floatArray;
valmo
  • 1,156
  • 2
  • 12
  • 22
  • 1
    You're just asking out of curiosity, aren't you? Since `&floatVector[0]` behaves just like your `floatArray`. – Benjamin Bannier Jul 25 '11 at 18:23
  • Sure you can do this but why? – AJG85 Jul 25 '11 at 18:30
  • @honk. i actually got a memory leak somewhere and i thought this might be it but i guessed wrong :( i also didnt do a delete in my project but passed floatArray to a instance variable. – valmo Jul 25 '11 at 18:41
  • If you want to find your memory leak (and your using linux) checkout valgrind in debug mode. It will give you the line where the problem is. – Tom Jul 25 '11 at 18:53

5 Answers5

4

std::copy doesn't allocate memory, you should do it yourself.

littleadv
  • 20,100
  • 2
  • 36
  • 50
2

If you don't want to allocate the array first then you may use a back_inserter iterator to push elements one by one into certain containers. For some containers this is less efficient (I think) but can be very handy sometimes.

#include<iterator>
#include<vector>
#include<deque>

std::vector<float> floatVector(10,1.0);
std::deque<float>  floatDeque; //memory not allocated

//insert elements of vector into deque.
std::copy(floatVector.begin(), floatVector.end(), std::back_inserter(floatDeque));
Tom
  • 5,219
  • 2
  • 29
  • 45
  • I'd never seen back_inserter before, there are so many nice utilities hidden away in the stl! Should `back_inserter` be `std::back_inserter`? – Greg Howell Jul 25 '11 at 18:46
  • @Greg, yep, corrected! There are a number of inserters available. If you want a nice usage, check this answer to my question (where I first learn't about them) http://stackoverflow.com/questions/5424093/how-to-iterate-into-a-smaller-container-i-e-stride-1/5424313#5424313 – Tom Jul 25 '11 at 18:50
  • Technically it will work without the explicit `std::` because `deque` is in the `std` namespace, so `back_inserter` would be found via ADL. That said, yes, you should definitely qualify the name both to avoid confusion and to avoid obscure ADL issues. :-) – James McNellis Jul 25 '11 at 21:02
0

copy will not allocate for you, so yes you need to allocate it.

Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106
0

std::copy copies by using the arguments assignment ("=") operator or the arguments copy constructor (this is probably implementation dependent, I'm not sure right now). It does nothing but iterate through the range beginning at param.begin() to param.end() and do something like:

while (first!=last) *result++ = *first++;

Thus, you need to allocate the memory necessary yourself. Otherwise it will create undefined behaviour.

Is it OK to copy a constant vector to an array this way? Depends on what you want to do. Generally, it's fine, since the values are transferred to the target by value, not by reference, hence const-correctness is not broken.

For example, this works fine:

std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
const std::vector<int> constvec(vec);

int *arrray = new int[2];
std::copy(constvec.begin(),constvec.end(),arrray);
TravisG
  • 2,373
  • 2
  • 30
  • 47
0

copy function never allocate memory but you have to allocate memory for static datastructures but for dynamic datastructures, it'll allocate memory automatically.

and this process is not good but its better to use another vector instead of that floatarray

Bhargava
  • 301
  • 5
  • 13