• Create Account

### #ActualServant of the Lord

Posted 31 October 2012 - 11:12 PM

Thanks, SiCrane, I think I understand it.

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[1]) Type();

//Allocate new memory.
Type *memoryB = (Type*) operator new(sizeof(Type) * 4);

//Copy data. The constructed object is now at memoryB[2].
memcpy(&memoryB[1], &memoryA[0], 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[2].~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.

### #1Servant of the Lord

Posted 31 October 2012 - 11:10 PM

Thanks, SiCrane, I think I understand it.

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[1]) Type();
//Allocate new memory.
Type *memoryB = (Type*) operator new(sizeof(Type) * 4);
//Copy data. The constructed object is now at memoryB[2].
memcpy(&memoryB[1], &memoryA[0], 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[2].~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?

PARTNERS