Hi,
I have a map containing pointers. When I destroy the map I want to
delete
all the pointers.
typedef std::map<std::s tring, const T*Table;
void destroy_map ()
{
for (Table::iterato r i = table_.begin(); i != table_.end(); ++i)
{
delete (*i).second;
table_.erase (i);
}
}
this crashes. Does the erase() invalidate the iterator?
This was the code I found on the net (latout fixed a bit)
for (map::iterator itr = myMap.begin(); itr != myMap.end())
{
if(itr->value == something)
myMap.erase(itr ++);
else
itr++;
}
which it is claimed is from Josuttis. I suppose the fact that it
didn't compile should of made me doubt this...
--
Nick Keighley
Nov 8 '07
17 50536
Nick Keighley <ni************ ******@hotmail. comwrote:
it seemed not unusual for a container to own an object it pointed
to. If you're newing a lot of similar objects then you need to keep
them somewhere. Why not a container?
There is some confusion here I think. When you say "you're newing" you
don't actually mean the programmer, rather some object is instantiating
some other object. Generally, the instantiating object is the owner.
On 9 Nov, 11:07, "Daniel T." <danie...@earth link.netwrote:
Nick Keighley <nick_keighley_ nos...@hotmail. comwrote:
it seemed not unusual for a container to own an object it pointed
to. If you're newing a lot of similar objects then you need to keep
them somewhere. Why not a container?
There is some confusion here I think. When you say "you're newing" you
don't actually mean the programmer, rather some object is instantiating
some other object. Generally, the instantiating object is the owner.
yes I meant an instantiating object was newing the Type objects.
Type is actually an abstract base class. The instantiator generates
many objects of various classes derived from Type. I can't see how
the parser *can* hold many Type objects without using a container of
some sort.
Simplifying outrageously:-
void parse_file (Token_stream& toks)
{
while (toks.more)
{
Type* type;
string name = toks.current_to ken;
toks.next();
// this is hidden in a factory
if (toks.current_t oken == "type_a")
type = new Type_a(toks);
else
if (toks.current_t oken == "type_b")
type = new Type_b(toks);
else
...
symbol_table.in ert(name, type);
}
}
In this case the parser which built the symbol table
ceases to exist once the table is built. So it can't
own the Type objects!
this seems such a natural idiom to me and falls naturally out
of the requirement. So I'm either abusing OOP/C++ or I don't
understand
what "ownership" means.
help!
--
Nick Keighley
Nick Keighley <ni************ ******@hotmail. comwrote:
"Daniel T." <danie...@earth link.netwrote:
Nick Keighley <nick_keighley_ nos...@hotmail. comwrote:
it seemed not unusual for a container to own an object it
pointed to. If you're newing a lot of similar objects then you
need to keep them somewhere. Why not a container?
There is some confusion here I think. When you say "you're
newing" you don't actually mean the programmer, rather some
object is instantiating some other object. Generally, the
instantiating object is the owner.
yes I meant an instantiating object was newing the Type objects.
Type is actually an abstract base class. The instantiator generates
many objects of various classes derived from Type. I can't see how
the parser *can* hold many Type objects without using a container
of some sort.
In this case the parser which built the symbol table ceases to
exist once the table is built. So it can't own the Type objects!
So the object that instantiates the parser takes ownership of the Type
objects. The point is though that the container didn't create the
objects, so it doesn't own them. The object that created the parser and
used it as a surrogate, indirectly created the Type objects and therefor
owns them.
On Nov 9, 12:07 pm, "Daniel T." <danie...@earth link.netwrote:
Nick Keighley <nick_keighley_ nos...@hotmail. comwrote:
it seemed not unusual for a container to own an object it
pointed to. If you're newing a lot of similar objects then
you need to keep them somewhere. Why not a container?
There is some confusion here I think. When you say "you're
newing" you don't actually mean the programmer, rather some
object is instantiating some other object. Generally, the
instantiating object is the owner.
Generally, in an OO design (which works well in many fields of
endevor), once created, an object is on its own. The object
itself decides when it's no longer needed, and deletes itself.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Thu, 08 Nov 2007 02:55:35 -0800, Nick Keighley wrote:
>I have a map containing pointers. When I destroy the map I want to delete all the pointers.
typedef std::map<std::s tring, const T*Table;
Std containers were designed only for values. They don't work with
pointers (to objects), at least not without clumsy and prohibitive
workarounds. This is one of the major reasons for the low acceptance
of STL in the real world (in contrast to the ongoing STL hype among a
small group of C++ gurus and "gurus").
So, better look for a container that works with pointers. You can even
built your own on top of an STL container.
BTW, std::string as key can introduce problems, too. Depending on your
basic_string implementation it may dynamically allocate an internal
buffer for each copy. This may be unacceptable for a real parser.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
On Fri, 09 Nov 2007 18:36:32 -0000, James Kanze wrote:
>Generally, in an OO design (which works well in many fields of endevor), once created, an object is on its own. The object itself decides when it's no longer needed, and deletes itself.
But not in C++. Actually, a dynamically created object cannot decide
on its own when it's no longer needed.
The classic C++ idiom is to let an 'owner object' manage dynamically
created objects, preferably by deleting them in its destructor, a.k.a.
RAII. The best working idiom in this context is 'Creator As Sole
Owner' http://www.ddj.com/184409895 .
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
On Thu, 08 Nov 2007 02:55:35 -0800, Nick Keighley wrote:
for (map::iterator itr = myMap.begin(); itr != myMap.end())
{
if(itr->value == something)
myMap.erase(itr ++);
else
itr++;
}
for(map::iterat or j, i = myMap.begin(); i != myMap.end(); i = j)
{
j = i; ++j;
if(i->value == something)
myMap.erase(i);
}
This is how I do it as I wasn't aware of erase() returning an
iterator (which apparently may be the case in some implementations ).
If you want to avoid the redundant default constructor
call of j, then write it as follows:
for(map::iterat or i = myMap.begin(); i != myMap.end(); )
{
map::iterator j = i; ++j;
if(i->value == something) myMap.erase(i);
i=j;
}
However, this code is more prone for errors: for example,
if you use "continue;" , it will omit "i=j;", causing an
infinite loop at worst.
--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
On Thu, 08 Nov 2007 02:55:35 -0800, Nick Keighley wrote:
for (map::iterator itr = myMap.begin(); itr != myMap.end())
{
if(itr->value == something)
myMap.erase(itr ++);
else
itr++;
}
for(map::iterat or j, i = myMap.begin(); i != myMap.end(); i = j)
{
j = i; ++j;
if(i->value == something)
myMap.erase(i);
}
This is how I do it as I wasn't aware of erase() returning an
iterator (which apparently may be the case in some implementations ).
If you want to avoid the redundant default constructor
call of j, then write it as follows:
for(map::iterat or i = myMap.begin(), j = i; i != myMap.end(); i = j)
{
++j;
if(i->value == something) myMap.erase(i);
}
--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ian McBride |
last post by:
(was: delete() confusion)
I have a class with multiple base classes. One of these base classes
(base1) has its own new/delete operators and nothing else. Another base
class (base 2) has a virtual destructor. The class with the virtual
destructor has AddRef() and Release() methods, and Release() ultimately does
a "delete this". Can that "delete this" statement somehow find the right
delete method? If it does, it involves a downcast...
|
by: Randy Gordon |
last post by:
Say I do the following:
char **names = new char*;
for (int i = 0; i < 100; i++) {
names = new char;
}
When I'm done with the array of character pointers, how should I delete it?
Like this:
|
by: Nimmi Srivastav |
last post by:
There's a rather nondescript book called "Using Borland C++" by Lee
and Mark Atkinson (Que Corporation) which presents an excellent
discussion of overloaded new and delete operators. In fact there are
quite a few things that I learned that I did not know before. For
example, while I knew that the new and delete operators can be
overloaded for classes, I did not know that that the global new and
delete operators can also be overloaded. ...
|
by: Nimmi Srivastav |
last post by:
There's a rather nondescript book called "Using Borland C++" by Lee
and Mark Atkinson (Que Corporation) which presents an excellent
discussion of overloaded new and delete operators.
I am presenting below a summary of what I have gathered. I would
appreciate if someone could point out to something that is specific to
Borland C++ and is not supported by the ANSI standard. I am also
concerned that some of the information may be outdated...
|
by: Christian Meier |
last post by:
Hi NG
I don't know if this problem is system dependend but I don't think so.
So, here is my assumption: "delete" deletes a variable. "delete" deletes
an array. Is this correct so far?
And now check this code out:
#include <iostream>
using namespace std;
| |
by: Stephen |
last post by:
Hello People,
Using MS Access 2003 VBA I get the error 3020 Update or CancelUpdate
without AddNew or Edit when I run through the following code. Can
anyone help suggest anything to try? Thanks.
On Error GoTo delete_failed
Dim RS_DEL As DAO.Recordset
|
by: junw2000 |
last post by:
Hi,
Below is a small code about memory allocation and deallocation.
#include <cstdlib>
#include <iostream>
using namespace std;
class X {
public:
void* operator new(size_t sz) throw (const char*) {
|
by: mkaushik |
last post by:
Hi everyone,
Im just starting out with C++, and am curious to know how "delete
<pointer>", knows about the number of memory locations to free.
I read somewhere that delete frees up space assigned to <pointerby
"new". Does "new" create a list of pointer names and the size of the
memory array they point to? I also read that some compilers may store
the number of consec mem locations a pointer points to, just before the
first data...
|
by: Gonçalo Rodrigues |
last post by:
Hi all,
I am a little confused about the delete operator, so I have a
question. Suppose we have something like
class Base {
public:
void* operator new(std::size_t size);
void operator delete(void* ptr);
|
by: rasmidas |
last post by:
I have a similar kind of code as below. But I could not guess how to delete. Please help me.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char* items;
int i;
for(i=0;i<5;i++)
|
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: 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |