Suppose there is pointer to an array of float values: float *source;
and we know its size as int sourcesize;
There is an already implemented function which add an element to the souce
array from the inputVec
:
void addRecord(const float* inputVec, int& sourcesize)
{
int inputVecSize = sourcesize;
memmove( (float*)&(source[inputVecSize]), inputVec, sizeof(float));
}
Now, I want to copy 1 element from the m
th element of the source array and attach it to the source end. By using the addRecord above, I have implemented a function as below:
// suppose m is smaller than the current sourcesize
void copyRecord(const float* source, int& m)
{
float* temporary = new float;
memcpy( temporary, (float*)&(source[m]), sizeof(float));
addRecord(temporary, sourcesize);
delete temporary;
}
It seems the memmove
call in the addRecord
function may share the variable location of temporary. Thus, maybe I should not delete temporary
in the end. But I think maybe they do not share the same address, then I should delete temporary
in this case.
So, should I delete temporary
in the end or not?
Or, is there a better way to copy an element from source
array to its end by using function addRecord
?