Hi, i have a std::vector of pointers to base classes say
std::vector<ele ment*m_elements ;
how do i make the followin exception safe
function()
{
element* e= new DerivedElement;
m_elements.push _back(element);
}
the push back operation can throw so i can leak element if it throws
i thought of doing this but it still has the same problem as e.get()
gets executed first
then if the push back throws i leak the pointer. How to solve?? I
can't use
Boost on this project so the shared_ptr is not an option
function()
{
std::auto_ptr <elemente(new DerivedElement) ;
m_elements.push _back(e.get());
} 18 2203
tech wrote:
Hi, i have a std::vector of pointers to base classes say
std::vector<ele ment*m_elements ;
how do i make the followin exception safe
function()
{
element* e= new DerivedElement;
m_elements.push _back(element);
}
the push back operation can throw so i can leak element if it throws
i thought of doing this but it still has the same problem as e.get()
gets executed first
then if the push back throws i leak the pointer. How to solve?? I
can't use
Boost on this project so the shared_ptr is not an option
Then roll your own simple shared_ptr pointer.
--
Ian Collins.
"tech" <na************ @googlemail.com wrote in message
news:01******** *************** ***********@z66 g2000hsc.google groups.com...
Hi, i have a std::vector of pointers to base classes say
std::vector<ele ment*m_elements ;
how do i make the followin exception safe
function()
{
element* e= new DerivedElement;
m_elements.push _back(element);
}
the push back operation can throw so i can leak element if it throws
i thought of doing this but it still has the same problem as e.get()
gets executed first
then if the push back throws i leak the pointer. How to solve?? I
can't use
Boost on this project so the shared_ptr is not an option
function()
{
std::auto_ptr <elemente(new DerivedElement) ;
m_elements.push _back(e.get());
}
You could do something like:
function()
{
std::auto_ptr <elemente(new DerivedElement) ;
m_elements.push _back(e.get());
e.release();
}
On Jun 11, 12:15*pm, "Chris Thomasson" <cris...@comcas t.netwrote:
"tech" <naumansulai... @googlemail.com wrote in message
news:01******** *************** ***********@z66 g2000hsc.google groups.com...
Hi, i have a std::vector of pointers to base classes *say
std::vector<ele ment*m_elements ;
how do i make the followin exception safe
function()
{
* *element* e= new DerivedElement;
* *m_elements.pus h_back(element) ;
}
the push back operation can throw so i can leak element if it throws
i thought of doing this but it still has the same problem as e.get()
gets executed first
then if the push back throws i leak the pointer. How to solve?? I
can't use
Boost on this project so the shared_ptr is not an option
function()
{
* std::auto_ptr <elemente(new DerivedElement) ;
* m_elements.push _back(e.get());
}
You could do something like:
function()
{
* *std::auto_ptr <elemente(new DerivedElement) ;
* *m_elements.pus h_back(e.get()) ;
* *e.release();
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Replying to Chris Thomasson
std::auto_ptr <elemente(new DerivedElement) ;
m_elements.push _back(e.get());
e.release();
But this is what i had above without the e.release() at the
bottom, if m_elements.push _back(e.get()); this throws
as the e.get() has already given up the pointer then if the
push back throws surely i will leak the pointer.
Or are you saying the e.get() returns a copy of the pointer
and the auto_ptr still hangs onto it. In that case
it would work and thanks to you
tech <na************ @googlemail.com kirjutas:
Hi, i have a std::vector of pointers to base classes say
std::vector<ele ment*m_elements ;
how do i make the followin exception safe
function()
{
element* e= new DerivedElement;
m_elements.push _back(element);
}
the push back operation can throw so i can leak element if it throws
i thought of doing this but it still has the same problem as e.get()
gets executed first
then if the push back throws i leak the pointer. How to solve?? I
can't use
Boost on this project so the shared_ptr is not an option
What about :
void function()
{
m_elements.push _back(NULL);
element*& ref = m_elements.back ();
ref = new DerivedElement;
}
HTH
Paavo
Alf P. Steinbach wrote:
* Paavo Helde:
>> What about:
void function() { m_elements.push _back(NULL); element*& ref = m_elements.back (); ref = new DerivedElement; }
If DerivedElement constructor throws, has already added nullpointer to
vector.
Cheers, & hth.,
- Alf
How about using a try/catch.
void function() {
element * d = new DerivedElement;
try {
m_elements.push _back(d);
} catch (...) {
delete d;
throw;
}
}
--
Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
"Alf P. Steinbach" <al***@start.no kirjutas:
* Paavo Helde:
>> What about:
void function() { m_elements.push _back(NULL); element*& ref = m_elements.back (); ref = new DerivedElement; }
If DerivedElement constructor throws, has already added nullpointer to
vector.
Yes, that's true. What I would do actually in this case, would be to use
the ScopeGuard from Andrei Alexandrescu. This is a case of glueing
together C-style interfaces (raw pointers!) and ScopeGuard comes quite
handy in such situations:
#include <ScopeGuard/ScopeGuard.h>
template <class T>
struct Delete {
void operator()(T *t) const { delete t;}
};
void function()
{
element* e = new DerivedElement;
ScopeGuard guard = MakeGuard(Delet e<element>(), e);
m_elements.push _back(e);
guard.Dismiss() ;
}
But this is only because I have the Delete template and ScopeGuard
includes already in place in the project anyway. std::auto_ptr would work
the same in this case (I suppose, never used myself) and would require
less code in this case.
Regards
Paavo
On Sun, 15 Jun 2008 16:30:19 +0200, "Alf P. Steinbach" wrote:
>* Roland Pibinger:
>2. You cannot practically 'handle' Out-Of-Memory (OOM).
Well I think I've written something like that in the past.
Well, some years ago I thought that you could handle OOM ...
>But it's wrong.
Think about an application where the user attempts to load a very big image file. If allocation fails, a good way to handle it is to inform the user that sorry, that file was too big. A bad way to handle it would be to terminate...
In case of a file you know the file size in advance and know if the
size is within the limits of your function contract. The Java language
distinguishes between (recoverable) Exceptions and (fatal) Errors.
What can you do after OOM? Can you recover vital memory to proceed?
Hardly in practice. IMO, OOM is a fatal error (like stack overflow and
memory corruption) that should lead to more or less abrupt termination
of the program. This also means that you need not write your code as
if std::bad_alloc were a recoverable exception.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Paavo Helde wrote: rp*****@yahoo.c om (Roland Pibinger) kirjutas:
>On Wed, 11 Jun 2008 03:36:07 -0700 (PDT), tech rote:
>>>Hi, i have a std::vector of pointers to base classes say std::vector< element*m_eleme nts; how do i make the followin exception safe
function() { element* e= new DerivedElement; m_elements.push _back(element); }
You question includes more than one aspect:
1. STL is designend for values only (a.k.a. 'value semantics'), not objects or pointers to objects. Put simply, STL doesn't work with pointers.
It seems this is so ridiculous no one has bothered to answer. For innocent
bystanders I just remind that pointers are values in C++.
It is true, though, that pointers often require special handling when
dealing with containers. The most basic issue is illustrated by
std::map< char const *, some_type >
By default, the map will compare pointer values and not the strings they
represent. Very likely that is _not_ the desired behavior.
Best
Kai-Uwe Bux
Kai-Uwe Bux <jk********@gmx .netkirjutas:
Paavo Helde wrote:
>rp*****@yahoo.c om (Roland Pibinger) kirjutas:
>>On Wed, 11 Jun 2008 03:36:07 -0700 (PDT), tech rote: Hi, i have a std::vector of pointers to base classes say std::vector <element*m_elem ents; how do i make the followin exception safe
function( ) { element* e= new DerivedElement; m_elements.push _back(element); }
You question includes more than one aspect:
1. STL is designend for values only (a.k.a. 'value semantics'), not objects or pointers to objects. Put simply, STL doesn't work with pointers.
It seems this is so ridiculous no one has bothered to answer. For innocent bystanders I just remind that pointers are values in C++.
It is true, though, that pointers often require special handling when
dealing with containers. The most basic issue is illustrated by
std::map< char const *, some_type >
By default, the map will compare pointer values and not the strings
they represent. Very likely that is _not_ the desired behavior.
If you specify the map keys as pointers, they will be compared as
pointers, that's it. I see nothing specific to STL here.
You could easily instruct the map to use the string comparison (if/when
needed) instead by providing an extra template argument for the map
declaration.
IOW, STL does what it is told to do. It does not attempt to read your
mind. This is a Good Thing IMO.
Regards
Paavo This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Adam Hartshorne |
last post by:
Hi All,
I have the following problem, and I would be extremely grateful if
somebody would be kind enough to suggest an efficient solution to it.
I create an instance of a Class A, and "push_back" a copy of this into a
vector V. This is repeated many times in an iterative process.
Ok whenever I "push_back" a copy of Class A, I also want to assign a
pointer contained in an exisiting instance of a Class B to this
|
by: Janiek Buysrogge |
last post by:
Hello,
I've written a Windows Forms application in .NET 2.0 and am exposing
it to COM using the checkbox in Project Properties and by adding some
register functions and adding it to the GAC. I'm using the ActiveX
Test Container (tstcon32.exe) and I'm able to add the control to the
container. All this works, well almost... I'm using some Invoke calls
from worker threads that update the GUI. In native .NET this means
that stuff is done on...
|
by: silversurfer |
last post by:
Ok, this should be fairly easy for most of you (at least I hope so),
but not for me:
Let us say we have got the following elements:
std::vector<Entry> models; //Entry is a struct
std::vector<Entry>::iterator modelIterator;
In a method, I am currently writing, I need to get a pointer to an
entry in the vector.. I thought of the following:
|
by: axel22 |
last post by:
Hello,
I have the following problem. I create a class called MyClass which
includes another class calles MyContainer. Class MyContainer has a
member which is a vector, instantiated as vector<MyClass>. When trying
to compile this I receive an error. Here is how it goes:
---------------
MyClass.h
---------------
|
by: Michael |
last post by:
Hi,
typedef std::vector<Vehicle* VehicleList;
Vehicle is a UDT, or a class. Why is Vehicle* not just Vehicle in < >.
Which one is better and why? If you could explain it by laying out some
codes, it would be highly appreciated!
Thanks in advance,
Michael
| |
by: toton |
last post by:
Hi,
all of the containers in STL stores object itself (thus copy
contsructability & assignability is needed), while NTL or boost
ptr_container stores pointer to the object in the container (either
exclusively owns, or just stores).
Now, my question is for a general guideline when to use which one?
What I understand,
1) polymorphic objects need ptr_container.
2) non copy constructable, non assignable objects need ptr_container.
|
by: Jess |
last post by:
Hello,
I tried a program as follows:
include<iostream>
using namespace std;
class A{
public:
|
by: Rob McDonald |
last post by:
I am interested in having a container which has properties of both the
STL's list and vector. (I want my cake and to eat it too).
In my application, I will need to add/remove items from arbitrary
points in the container.
I will also need to be able to perform random access to elements of
the container -- accessed by index, not associatively.
Fortunately, in my application, I don't need to do both of these
|
by: mathieu |
last post by:
Hi there
I have implemented a very simple smartpointer class (invasive
design). And I was wondering what should be the natural API when using
those in a Container.
I choose to define the following operator in my smartpointer class:
....
operator ObjectType * () const
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |