Easy question(on memmove)..but dumb Java guy here..

i am a bit confused..

i have two pointers, foo1 already has stuff in it, and i want to move the contents to foo2.
1
2
3
4
5
6
7
8
9
10
std::string *foo1 = ...has stuff....;
std::string *foo2;

..
..

memmove(foo2,foo1,sizeof(foo1));

..
cout<< foo2->c_str();


ABOVE WORKS!

however, i assume after the moving of content to mememory space of foo2, i can delete foo1 correct???
1
2
3
4
5
6
7
8
9
10
11
12
std::string *foo1;
std::string *foo2;

..
..

memmove(foo2,foo1,sizeof(foo1));

delete(foo1);

..
cout<< foo2->c_str();

Above doesn't work! foo2 is deleted as well, or empty....how do i move the foo1 to foo2, and able to delete foo1 because i dont neeed it anymore?

Thanks.

***Updated***, i didn't mean to type delete(foo2), what im saying is when i delete foo1, i try to access foo2->c_str() it doesn't work......

also, this is VERY simplified from what i am currently working on..so the code is Pseudocode..... in 'reality' foo1 and foo2 are structs, i am building up data in foo1 struct, when it is done filling up, i want to copy the content to foo2, but i want to delete the cotent of foo1 because don't need it anymore. and don't want to huge struct with data in memory.



Last edited on
All this business with memmove is nonsense. You can't just copy the bytes of a non-POD object and expect it to work (let alone a portion of it, which is what you're doing).
ABOVE WORKS!

No, of course not. It's undefined behavior.

The answer depends on what you're really trying to do.
Solution 1:
1
2
delete foo2;
foo2=foo1;


Solution 2:
1
2
*foo2=*foo1; //copies the string
delete foo1;


Solution 3:
1
2
swap(*foo2,*foo1); //saves you the copy
delete foo1;


Real solution:
1
2
3
4
5
std::string foo1;
std::string foo2;
foo2=foo1;
//or with C++11, you can literally move:
foo2=std::move(foo1);
Last edited on
I ran your code and my compiler crashed.

This article may interest you:

http://icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html

You could just do this:

 
foo1 = nullptr;

As far as I know you use delete when you initialize something with new.

The article above is very informative about the assignment operator, delete and operator overloading.

Last edited on
@iDontGetIt, Thanks for the link, it was very helpful.

@Athar, I am using 'swap' now, and maked foo1 = null, then call delete & free.
delete and free? What? Why?
Last edited on
1
2
3
4
swap(*foo2,*foo1); 
foot1 = NULL;
delete foo1;
free(foo1);



is what i am doing...is the free(foo1) necessary?
is the free(foo1) necessary?

It's not unnecessary, it's wrong. First, free is a C function and can only be used on a pointer that was returned by the function malloc. Second, even if it were allowed, you'd still be freeing the same memory twice. Third, you set foo1 (assuming it was a typo) to NULL prior to that anyway, meaning the following two lines have zero effect.

Rule of thumb: If you're using free in C++, you're doing it wrong.
If you're using delete or delete[], you're usually also doing it wrong. Look up RAII.
@Athar,

the code is very simplified.

the foo1 is a pointer that was created via malloc, thats why i assumed usning free(foo1) is ok...but if i understood your post..i simple need to call just delete, or NULL?

1
2
3
foo1 = malloc.....
swap(*foo2,*foo1); 
delete foo1;
For a pointer returned by malloc, you MUST use free.
For new, you must use delete. For new[], you must use delete[].
They are not interchangeable. Let alone are you allowed to call several of them on the same pointer.
@Athar, Understood. Thanks.
Topic archived. No new replies allowed.