I try to use the auto_ptr in my code but that doesn't work.
even std::auto_ptr.
I guess I have to add an #include statement, but I can't figure out the
right file to add.
Also I want to use auto_ptr not to manage a single int but and int array.
something like
std::auto_ptr<int> adv = new int[42];
but from what I read auto_ptr doesn't seems to be the right solution for the
job.
basically I want to use auto_ptr so if I have exception in a constructore or
in a method, all memory is cleanly deallocated.
without me bothering writing heaps of try/catch/finally
any tip?
I'm working in Managed C++ 8 1135
Lloyd Dupont wrote: I try to use the auto_ptr in my code but that doesn't work. even std::auto_ptr. I guess I have to add an #include statement, but I can't figure out the right file to add.
The MSDN says it's <memory>.
Also I want to use auto_ptr not to manage a single int but and int array. something like std::auto_ptr<int> adv = new int[42];
auto_ptr can't be used on plain arrays, because it calls delete, and
arrays need delete[]. You can instantiate std::vector instead of int[]:
std::auto_ptr<std::vector<int> > adv = new std::vector<int>(42);
(*adv)[2] = 2;
adv->operator[](3) = 3;
adv.get()->at(4) = 4;
If this vector has a function scope (it isn't used outside the function)
you can instantiate it on the stack:
std::vector<int> adv(42);
adv[1] = 1;
I'm working in Managed C++
I'm not, but it's the same (I believe).
Mihajlo Cvetanoviæ wrote: std::auto_ptr<std::vector<int> > adv = new std::vector<int>(42);
Sorry, that's
std::auto_ptr<std::vector<int> > adv( new std::vector<int>(42) );
Lloyd Dupont wrote: I try to use the auto_ptr in my code but that doesn't work. even std::auto_ptr. I guess I have to add an #include statement, but I can't figure out the right file to add.
#include <memory> Also I want to use auto_ptr not to manage a single int but and int array. something like std::auto_ptr<int> adv = new int[42];
std::vector<int> adv(42);
but from what I read auto_ptr doesn't seems to be the right solution for the job.
No, it only handles single elements.
Tom
thanks all!
--
There are 10 kinds of people in this world. Those who understand binary and
those who don't.
"Mihajlo Cvetanoviæ" <ma*@RnEeMtOsVeEt.co.yu> wrote in message
news:e6**************@TK2MSFTNGP14.phx.gbl... Mihajlo Cvetanoviæ wrote: std::auto_ptr<std::vector<int> > adv = new std::vector<int>(42);
Sorry, that's std::auto_ptr<std::vector<int> > adv( new std::vector<int>(42) ); std::auto_ptr<std::vector<int> > adv = new std::vector<int>(42);
vector is a dynamic array already, what's the point allocating an additional
pointer to it? Unless you wanted to demostrate the usage of auto_ptr, I
can't see a reason doing it that way.
Gabest wrote: vector is a dynamic array already, what's the point allocating an additional pointer to it? Unless you wanted to demostrate the usage of auto_ptr, I can't see a reason doing it that way.
The vector's assignment operator always copies elements from source to
destination, and vector by itself doesn't have detaching ability (to
reattach the inner array to another vector). If you have huge vectors
this copying (when you actually want "transfer of ownership") may hinder
your performance.
Apart from performance I can't think of any other reason.
Mihajlo Cvetanoviæ wrote: Gabest wrote:
vector is a dynamic array already, what's the point allocating an additional pointer to it? Unless you wanted to demostrate the usage of auto_ptr, I can't see a reason doing it that way.
The vector's assignment operator always copies elements from source to destination, and vector by itself doesn't have detaching ability (to reattach the inner array to another vector). If you have huge vectors this copying (when you actually want "transfer of ownership") may hinder your performance.
Actually, vector does have a detaching ability - v2.swap(v1);
Tom
Tom Widmer [VC++ MVP] wrote: Mihajlo Cvetanoviæ wrote:
The vector's assignment operator always copies elements from source to destination, and vector by itself doesn't have detaching ability (to reattach the inner array to another vector). If you have huge vectors this copying (when you actually want "transfer of ownership") may hinder your performance.
Actually, vector does have a detaching ability - v2.swap(v1);
Oh, <blushing>, then I guess there's no reason to use it that way...
But what if I want to assign vector to a list box item data? Or if I
want to pass a vector to a new thread? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Morgan Cheng |
last post by:
Below code has output
Foo created 3
0
doit
Foo destructed 3
I am confused. When an auto_ptr object is assigned to another auto_ptr
object, the control of pointer is transferred from orginal...
|
by: guru.slt |
last post by:
Hi,
see this code:
auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(int_auto_p.get());
Then, both auto_pointer will own the same object. That will violate the
single...
|
by: Lloyd Dupont |
last post by:
how do I redefine the new operator?
for all the structure I use at once!
(some of them comes from C include files).
The rationale:
I'm writting a managed C++ wrapper around C API (external...
|
by: Joshua Rulz |
last post by:
Hi, i want to learn to program im quite skilled with computers and want
to learn c++. is there anyone who can teach me or tell me a good
website to learn it?
all replies will be appreciated.
|
by: Yong Hu |
last post by:
Does the smart pointer never lead to memory leak?
Thanks,
|
by: Binary |
last post by:
Hi,
I am reading a chinese book about STL, the book says below code will
have no memory leak. I think its magic and wonder why the auto_ptr
knows the memory should be freed when leaving the...
|
by: timlyee |
last post by:
int *p = new int;
auto_ptr<intap1 = p; //will fail on 3 1
auto_ptr<intap1(p); //ok 2
*ap1 = 12; // 3
the first situation has called :...
|
by: dolphin |
last post by:
Hi All
Today , I read the source code of auto_ptr. I have a question
about the copy constructor of auto_ptr.
template<class _Ty>
class auto_ptr
{
..................
}
there is a copy...
|
by: Daniel T. |
last post by:
Carter <cartercheng@gmail.comwrote:
When you see "discards qualifiers", more often than not, it means that a
const object is begin asked to do something that only non-const objects
can do....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |