I have a few more questions though! What about moving constructed objects between two locations in memory?
If moving an element to a new location in memory, std::move() would not be the right choice, would it? std::move() doesn't actually move memory, right? It's an unrelated topic, repossessing already-used memory but for a different variable of the same type (to avoid an uneccesary construction, assignment, and destruction)?
Should I use memcpy() to blast the bits between two different locations in memory?
After using memcpy(), I don't need to destruct the old memory location, right?
Before using memcpy, I don't need to construct the new memory location, right?
Basically, is this correct:
//Allocate memory. Type *memoryA = (Type*) operator new(sizeof(Type) * 3); //Construct type. new (&memoryA) Type(); //Allocate new memory. Type *memoryB = (Type*) operator new(sizeof(Type) * 4); //Copy data. The constructed object is now at memoryB. memcpy(&memoryB, &memoryA, sizeof(Type) * 3); //Delete old location (without destructing). delete memoryA; //How's it know how much memory to delete? Do I need to cast back to void* before calling delete? //Destruct type. memoryB.~Type(); //Delete data. delete memoryB;
How would "delete memoryA" know how much memory to delete? Do I need to cast back to void* before calling delete in that situation?
[Edit:] Yes, I can store it as Type*, don't know why I was thinking char*. Probably from data packets and file reading where the data isn't a uniform type.