realted to new operator.
-
Hi all, can any one explain me what is the difference in the following code. 1.I have a class CSample. 2.I want to create an object on heap by using new operator for CSample. I found there are two methods for this. 1.One is like this CSample * pSamep=new CSample; and 2.second one is like this typedef auto_ptr pSample; pSample(new CSample()); what is the difference with these two methods.which one is more efficient. thanks in advance. regards raju anju
-
Hi all, can any one explain me what is the difference in the following code. 1.I have a class CSample. 2.I want to create an object on heap by using new operator for CSample. I found there are two methods for this. 1.One is like this CSample * pSamep=new CSample; and 2.second one is like this typedef auto_ptr pSample; pSample(new CSample()); what is the difference with these two methods.which one is more efficient. thanks in advance. regards raju anju
anju wrote: typedef auto_ptr pSample; I assume you meant: typedef auto_ptr<CSample> pSample; The auto_ptr is a small structure that holds the pointer to the object, and a flag indicating whether or not it owns the object (it does by default). In it's destructor, it deletes the object if it owns it. This can greatly simplify memory management, as these smart pointers take care of deleting themselves when they go out of scope. In terms of efficiency, the auto_ptr would be every so slightly less efficient, but we are probably talking literally one or two CPU instructions, so it is really irrelevant. Dave http://www.cloudsofheaven.org
-
anju wrote: typedef auto_ptr pSample; I assume you meant: typedef auto_ptr<CSample> pSample; The auto_ptr is a small structure that holds the pointer to the object, and a flag indicating whether or not it owns the object (it does by default). In it's destructor, it deletes the object if it owns it. This can greatly simplify memory management, as these smart pointers take care of deleting themselves when they go out of scope. In terms of efficiency, the auto_ptr would be every so slightly less efficient, but we are probably talking literally one or two CPU instructions, so it is really irrelevant. Dave http://www.cloudsofheaven.org
-
I have tried the follwing : typedef auto_ptr str; but is not working. Gives error "syntax error : missing ';' before '<'". Is any header file needed? Chintan C.R.Naik
I think it is in the <memory> header file, but it is also defined within the std namespace. If you repost your error message, take not that codeproject thinks that everything between angle brackets is an HTML tag, and so does not display it. You should use < and > Dave http://www.cloudsofheaven.org
-
I think it is in the <memory> header file, but it is also defined within the std namespace. If you repost your error message, take not that codeproject thinks that everything between angle brackets is an HTML tag, and so does not display it. You should use < and > Dave http://www.cloudsofheaven.org
-
previous time I have tried : typedef auto_ptr < CString > str; and again I have tried the following one : typedef std::auto_ptr < CString > str; but both are not working and giving error Chintan C.R.Naik
Are you including <memory> rather than <memory.h>? Dave http://www.cloudsofheaven.org
-
Are you including <memory> rather than <memory.h>? Dave http://www.cloudsofheaven.org
-
anju wrote: typedef auto_ptr pSample; I assume you meant: typedef auto_ptr<CSample> pSample; The auto_ptr is a small structure that holds the pointer to the object, and a flag indicating whether or not it owns the object (it does by default). In it's destructor, it deletes the object if it owns it. This can greatly simplify memory management, as these smart pointers take care of deleting themselves when they go out of scope. In terms of efficiency, the auto_ptr would be every so slightly less efficient, but we are probably talking literally one or two CPU instructions, so it is really irrelevant. Dave http://www.cloudsofheaven.org
I thought it might be useful to explain a little more of the reason why auto_ptr was included in the standard. (There was a great deal of haggling about this and other types of pointer wrappers, and there are a number of other pointer wrappers in boost (boost.org) that will almost certainly make it into the next version of the standard.) The reason why auto_ptr is important relates to exceptions. Consider this: class excep { /* some exception class */}; void g() { try { f(); } catch(excep &e) { handle(e); } } void f() { int* a = new int; *a = might_throw_excep(); other_fn(); delete a; }; Now consider what happens if might_throw_excep throws an exception. In that case, the stack unwinds to the nearest matching catch statement (in g), but that means that a, which is now out of scope, points to heap allocated memory that will never be deleted. auto_ptr solves this problem, by rewriting f() as the following: void f() { auto_ptr a = new int; *a = might_throw_excep(); other_fn(); } In this case, the destructor of a (which is an auto_ptr template class) is called when it goes out of scope. This causes the object a to be deleted. However, what is more important is this: even if might_throw_excep throws, then as part of the exception processing system, the destructors of the classes are called as the stack is unwound to the catch statement. This happens even though other_fn is not called. That means that, even if an exception is thrown, a's destructor is called, and it doesn't leak memory. For that reason, auto_ptr (or something like it) is necessary to write leak free code in the presence of exceptions. This is the motivation, AFAIK, behind why auto_ptr was added to the standard. HTH.