F
Frederick Gotham
(Before I begin, please don't suggest to me to use "std::vector" rather
than actual arrays.)
I understand that an object can have resources (e.g. dynamically
allocated memory), and so we have to copy an object properly via copy-
construction rather than using memcpy.
However, is it okay to move an object manually (i.e. by using memcpy or
memmove)?
I have an algorithm for rotating an array. It turns:
A B C D E F G
into:
B C D E F G A
and if run a second time, that would become:
C D E F G A B
I intend for it to be used on anything from and array of char's, to an
array of vector's.
Is there anything wrong with the way it moves objects around?
Here's some sample code:
#include <iostream>
using std::cout;
#include <cstdlib>
using std::memcpy;
#include <cstring>
using std::memmove;
template<class T>
void RotateArrayOnce( T * const p_start, T * const p_over )
{
unsigned long const len = p_over - p_start;
unsigned char * const p_temp_storage = new unsigned char[ sizeof(T)
];
memcpy( p_temp_storage, p_start, sizeof(T) );
memmove( p_start, p_start + 1, sizeof(T) * (len - 1) );
memmove( p_over - 1, p_temp_storage, sizeof(T) );
delete [] p_temp_storage;
}
int main()
{
char buffer[] = "ABCDE";
cout << buffer << '\n';
RotateArrayOnce(buffer, *(&buffer + 1) - 1 );
cout << buffer << '\n';
RotateArrayOnce(buffer, *(&buffer + 1) - 1 );
cout << buffer << '\n';
std::system("PAUSE");
}
At first thought, I don't see how it could be a problem to simply move an
object in memory (assuming the target location is suitably aligned).
Only thing I can see which could make things go awry is if the object
kept its own address stored within it for some reason.
than actual arrays.)
I understand that an object can have resources (e.g. dynamically
allocated memory), and so we have to copy an object properly via copy-
construction rather than using memcpy.
However, is it okay to move an object manually (i.e. by using memcpy or
memmove)?
I have an algorithm for rotating an array. It turns:
A B C D E F G
into:
B C D E F G A
and if run a second time, that would become:
C D E F G A B
I intend for it to be used on anything from and array of char's, to an
array of vector's.
Is there anything wrong with the way it moves objects around?
Here's some sample code:
#include <iostream>
using std::cout;
#include <cstdlib>
using std::memcpy;
#include <cstring>
using std::memmove;
template<class T>
void RotateArrayOnce( T * const p_start, T * const p_over )
{
unsigned long const len = p_over - p_start;
unsigned char * const p_temp_storage = new unsigned char[ sizeof(T)
];
memcpy( p_temp_storage, p_start, sizeof(T) );
memmove( p_start, p_start + 1, sizeof(T) * (len - 1) );
memmove( p_over - 1, p_temp_storage, sizeof(T) );
delete [] p_temp_storage;
}
int main()
{
char buffer[] = "ABCDE";
cout << buffer << '\n';
RotateArrayOnce(buffer, *(&buffer + 1) - 1 );
cout << buffer << '\n';
RotateArrayOnce(buffer, *(&buffer + 1) - 1 );
cout << buffer << '\n';
std::system("PAUSE");
}
At first thought, I don't see how it could be a problem to simply move an
object in memory (assuming the target location is suitably aligned).
Only thing I can see which could make things go awry is if the object
kept its own address stored within it for some reason.