Multiple serial-dependent allocations

Started by
23 comments, last by Jan Wassenberg 18 years, 9 months ago
I am terribly, terribly sorry, just didn't quite think it through.
Given the semantics of std::auto_ptr, though, you could probably declare local auto_ptrs in the function, check for errors at the end of the function, and if there are no errors, pass ownership of the objects to permanent storage, something like this:
MyObject *ob1, *ob2, *obn;bool MyLoadFunc(){std::auto_ptr<MyObject> ptr1, ptr2, ptr3;if (!(ptr1 = new MyObject()))return false;if (!(ptr2 = new MyObject()))return false;if (!(ptrn = new MyObject()))return false;ob1 = ptr1.release();ob2 = ptr2.release();obn = ptrn.release();return true;}

i.e. if everything loads successfully, we remove control from the auto_ptrs, and assign them to the global (or whatever) pointers, and then they don't delete the resources at the end of the scope.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement
Quote:Original post by Zahlman
What good are these "move semantics" of auto_ptr anyway? Why didn't anyone think to include the equivalent but with "copy semantics" in the standard library as well (and perhaps an array version as well)? Perhaps policy-based design (like std::auto_ptr<typename T, typename AssignmentPolicy>) would be useful here?

They are good for doing the things such as those that are listed in the link I posted above.

Considering why auto-pointer has move-sematics: When you copy an auto-pointer to another auto-pointer, the contained pointer is moved and the source becomes NULL. Now consider if you were to copy the pointer. The old auto_ptr no longer has ownership (it has moved), so basically it acts like a regular pointer.

And this is the reason that auto_ptr moves, rather than copies. If auto_ptr were to act like a regular pointer, it would not only require an extra flag (and check) in auto_ptr to implement, it is rediculiously more confusing than the copy-is-move sematics of the auto_ptr because now we can have variables of type auto_ptr that may or may not own the object in question. Leaving the programmer to puzzle which one must not be let out of scope.

Of course, if auto_ptr copied both pointer and ownership, then it would be a shared pointer, not an auto pointer.
CoreMeltdown,

I think the best solution is your third one. The allocated objects cannot exist independently, so their allocation and deallocation should be done in the same place at the same time.

One other thing -- since the pointers are accessible externally, you should set them to 0 after deleting.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
blah * a = null;blah * b = null;blah * c = null;bool init(){	if (a = new blah && a->init())	{		if (b = new blah && b->init())		{			if (c = new blah && c->init())				return true;			delete c;			c = null;		}		delete b;		b = null;	}	delete a;	a = null;	return false;}

This is how I'd handle it. I'm not 100% sure I understood the question, though, so forgive me if I'm completely OT [grin]
I would hesitate to call setjmp a safe idiom to use because it magically jumps across functions/stack frames, messes up signals and in general suffers from the same problems as exceptions.

The standard C way to do it is with a bailout ladder:
err = do1()if(err < 0)	goto exiterr = do2()if(err < 0)	goto exit_undo1return 0;// successexit_undo1:undo1();exit:return err;


Deyja's code basically does the same thing but goes through gyrations to avoid goto (apparently only for the sake of doing so).
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3

This topic is closed to new replies.

Advertisement