Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C++

Destrying and object without calling a destructor

 
Jump to:  
 
Bruno.DiStefano
PostPosted: Wed Sep 03, 2008 8:58 am    Post subject: Destrying and object without calling a destructor
       
Hi All,

I have a large number of objects living in a double loop (see below,
where CYCLES and NUMBER are huge).

////////////////////////////////////////////////////////////////
for (t=0; t < CYCLES; t++)
{
for(i=0; i<NUMBER;i++)
{
MyClass[i].run(t);
}
}
////////////////////////////////////////////////////////////////

Along the way some objects should die and vanish from my execution. I
have no way of knowing which ones and I do not care. The output of my
program is an ascii file to be processed with other programs. I just
need to make sure that the objects which are supposed to die actually
die and that all memory associated to them is released, without
altering the order in which the other objects are stored. I do not
need a list. I am using and really need a vector. Given that I should
not call a destructor explicitely, what can I do?

Thank you.

Best regards

Bruno

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Thomas Richter
PostPosted: Wed Sep 03, 2008 7:21 pm    Post subject: Re: Destrying and object without calling a destructor
       
Bruno.DiStefano schrieb:
Quote:
Hi All,

I have a large number of objects living in a double loop (see below,
where CYCLES and NUMBER are huge).

////////////////////////////////////////////////////////////////
for (t=0; t < CYCLES; t++)
{
for(i=0; i<NUMBER;i++)
{
MyClass[i].run(t);
}
}
////////////////////////////////////////////////////////////////

Along the way some objects should die and vanish from my execution. I
have no way of knowing which ones and I do not care. The output of my
program is an ascii file to be processed with other programs. I just
need to make sure that the objects which are supposed to die actually
die and that all memory associated to them is released, without
altering the order in which the other objects are stored. I do not
need a list. I am using and really need a vector. Given that I should
not call a destructor explicitely, what can I do?

Thrust the vector implementation to do that? I don't quite understand the question,
the vector *does* destroy all the objects it holds.

So long,
Thomas

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Erik Wikström
PostPosted: Wed Sep 03, 2008 8:53 pm    Post subject: Re: Destrying and object without calling a destructor
       
On 2008-09-03 10:58, Bruno.DiStefano wrote:
Quote:
Hi All,

I have a large number of objects living in a double loop (see below,
where CYCLES and NUMBER are huge).

////////////////////////////////////////////////////////////////
for (t=0; t < CYCLES; t++)
{
for(i=0; i<NUMBER;i++)
{
MyClass[i].run(t);
}
}
////////////////////////////////////////////////////////////////

Along the way some objects should die and vanish from my execution. I
have no way of knowing which ones and I do not care. The output of my
program is an ascii file to be processed with other programs. I just
need to make sure that the objects which are supposed to die actually
die and that all memory associated to them is released, without
altering the order in which the other objects are stored. I do not
need a list. I am using and really need a vector. Given that I should
not call a destructor explicitely, what can I do?

If the objects are stored in the vector you can not free the memory they
occupy (without reordering) without destroying the vector. So you should
not store the objects in the vector, instead store (smart)pointers to
the actual objects , when an objects "dies" you can delete it and set
the pointer to 0.

Or you can redesign your objects using something like the PIMPL ideom so
you can free the majority of the memory used when an object is marked as
"dead".

--
Erik Wikström


[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Mathias Gaunard
PostPosted: Wed Sep 03, 2008 8:58 pm    Post subject: Re: Destrying and object without calling a destructor
       
On 3 sep, 10:58, "Bruno.DiStefano" <Bruno.DiStef...@gmail.com> wrote:

Quote:
Along the way some objects should die and vanish from my execution. I
have no way of knowing which ones and I do not care.

That's not how it works. Object lifetime is to be well defined in
terms of ownership.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

 
Hyman Rosen
PostPosted: Wed Sep 03, 2008 9:44 pm    Post subject: Re: Destrying and object without calling a destructor
       
Bruno.DiStefano wrote:
Quote:
I do not need a list. I am using and really need a vector.
Given that I should not call a destructor explicitely, what
can I do?

It very much depends on the nature of your classes and
the time at which individual objects need to be destroyed.
The simplest thing you can do is to call
MyClass.erase(MyClass.begin() + i);
when you need to destroy element i. But this will slide
all the remaining elements up one slot.

--
[ See LINK for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

przepisy kulinarne Mieszkania Kraków śmieszne rzeczy zakłady bukmacherskie Życzenia imieninowe