Loosing pointer values

I have a function that I pass a couple of parameters to. This function works as a wrapper to a third party library function. The results of the calculations are uploaded in a "results" pointer/array (see code below). Now, because of the the way that third party library works, the end result is not in the order in which I want it (there is some manipulation that happens in the third party library that returns it like that and I cannot change it). So, I copy the results into another pointer using the std::copy function.
That is all good and nice, it returns the results in the order in which I want them. The problem is if I pass the pointer returned by my function with the results to a different function for further manipulation of the data, all the data is lost (??) then and I cannot do any more calculations with it. When I don't do the "copy" step it works as intended and I can pass the array to other functions. So, my question is, is there something happening with the copy function? Do I need to reset the pointer to the original position?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
double *myFunction(double myArray[], int mySize){
	double *results = new double[mySize];
	double *copyToReturn = new double[mySize];
	int startPoint;
	int endCopying;
	
	/*
		calculations happen here and feed 
		the "results" array, variables endCopying and startPoint		
	*/
	std::copy(results,results + endCopying;, copyToReturn + startPoint);
		
	return copyToReturn;
}
In line 2 you allocate memory that you never free.
Besides that and the fact that there is a ";" too much after "endCopying" in line 11 the code looks fine.

You return the pointer copyToReturn but the data is copied to copyToReturn + startPoint. Is that ok?

Maybe you should step through the program or print debug messages containing the values of startPoint and endCopying.
Thanks for your reply. The ";" extra is just a typo. Apologies, it was too early this morning. It should read:

 
std::copy(results,results + startPoint, copyToReturn + endCopying);


Basically that just tells the copy function where to start and end copying the values from results. I had already tried releasing the "results" memory. Also, I tried debugging through it and printing the values in the array/pointer. Always the same result... the array looses the values as soon as I pass it to another function to do some more calculations. If I don't use the copy function then it works fine. Not sure what is going on.
If you can see the correct values in the copyToReturn array inside the function, but they disappear when you try to access them in another function, then I would guess that you are writing the values outside of the memory allocated in line 3. If it is outside of the allocated memory then it may be overwritten at any time. You have both results and copyToReturn the same size, but results is copied into copyToReturn at an offset (copyToReturn + endCopying). If mySize is just the right size for results, then copyToReturn must be at least mySize + endCopying in size, or you will be writing beyond the end of the array.

Are you giving the proper arguments to the copy function?


http://www.cplusplus.com/reference/algorithm/copy/

Copies the elements in the range [first,last) into a range beginning at result.

Returns an iterator to the last element in the destination range.

The behavior of this function template is equivalent to:

1
2
3
4
5
6
template<class InputIterator, class OutputIterator>
  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
{
  while (first!=last) *result++ = *first++;
  return result;
}
Topic archived. No new replies allowed.