Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Moving objects in C++

Name: Cudder !MhMRSATORI 2015-02-03 11:43

C++ has new (constructor), delete (destructor), but what happened to reallocating and moving, something that is so easily done in C?

What gets in the way is that there may be pointers to the moved object, and this is the most common argument against it. In C++11 "move semantics" were introduced which is far more complex and convoluted than the bleedingly obvious solution of... update the pointers to the moved object!

In other words, they could just add a special member function that is called when an object is moved, with one parameter - its new address.

class Bar;
class Foo {
Bar *b;
Foo() { b = 0; }
Foo(Bar *b) {
this->b = b;
}
void move(void *x) {
b->f = x;
}
};

class Bar {
Foo *f;
Bar() { f = 0; }
Bar(Foo *f) {
this->f = f;
}
void move(void *x) {
f->b = x;
}
};


If a Foo is moved, then its Bar needs to be updated, and vice-versa. Of course this doesn't work in all cases because not all objects can know what points to them, but for those that do, the solution is trivial and it makes realloc work. To not provide a language feature for moving any object, just because some of them can't be moved, is absurd.

Name: Anonymous 2015-02-03 11:52

2015
still not grokking functional programming

Name: Anonymous 2015-02-03 14:29

Sepples is pig-disgusting bloatware.

Name: Anonymous 2015-02-03 15:56

Must I at length the sword of justice draw?
Oh cursed effects of necessary law!
How ill my fear they by my mercy scan,
Beware the fury of a patient man.

Name: Anonymous 2015-02-03 19:28

this doesn't work in all cases because not all objects can know what points to them
Time to re-invent GC!

Name: Anonymous 2015-02-03 22:19

>>1
To not provide
Got tired of putting "I'm not a native english speaker" excuse everytime?

Name: Anonymous 2015-02-04 11:27

>>1
Oh god, you're literally retarded.

The whole and only purpose of move semantics is to avoid moving memory. Like, actually copying it over. Because copying memory over is slow. And for copying memory we've already had copy ctors.

Daaaaamn son, your post is the stupidest thing I've read in the past three days, if not the whole week.

How to impress friends and acquaintances with unbearable idiocy in three easy steps: 1) find a subject matter you know nothing about, 2) invent an elaborate theory of what it is about based on its name, 3) post a solution that solves that imaginary thing better.

IHBT

Name: Anonymous 2015-02-04 11:32

>>1
I just can't, I lost my ability to can.
In other words, they could just add a special member function that is called when an object is moved, with one parameter - its new address.
C(const C& other) {
printf("My new address is %p", this);
}

http://ideone.com/3z4Bji

How do you even? Because now I can't even.

Name: Anonymous 2015-02-04 12:46

>>7
Don't go too hard on Cudder, (s)he's just not a programmer by profession.

Name: Cudder !MhMRSATORI 2015-02-04 16:41

>>7,8
A copy constructor is for creating an entirely new copy of an object, not moving an existing one. Sometimes an existing one must be moved. That's why this is more of a "move updater".

Name: Anonymous 2015-02-04 17:57

>>10
A copy constructor is for creating an entirely new copy of an object, not moving an existing one. Sometimes an existing one must be moved.
Are you aware that you can't actually "move" memory? Like, when you want to relocate an object, you copy it somewhere, then update internal pointers?

Name: Anonymous 2015-02-04 20:49

>>11
Bullshit! I can rewire core without losing remanence.

Name: Anonymous 2015-02-05 0:31

>>7,8,11
you fucking idiots need to learn to program in C. You'll never understand how to balance on a bike when you've used training wheels your whole life.

Name: Anonymous 2015-02-05 1:20

It is a bad programming practice to move or copy anything after the program was initialized. You should preallocate all required structures at the beginning of the program. And when you need to change ordering of elements in memory, just use pointers.

Name: Anonymous 2015-02-05 1:29

No, Cudder. No.

Name: Anonymous 2015-02-05 1:45

>>1
You talk and talk and talk, but have you ever written a non-trivial program?

Name: Anonymous 2015-02-05 1:47

>>15,16
If it's a waste of time to read what you write, don't post.

Don't change these.
Name: Email:
Entire Thread Thread List