473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

delete in-place corresponding to placement new?

I have just built a class that provides the most useful subset of std::vector
functionality for use by compilers that lack template capability.

http://home.att.net/~olcott/std_vect.html

Through suggestions from this news group I was able to exactly duplicate
the interface of std::vector, except for one aspect. What I need is a way
to invoke the destructor on elements of an array without de-allocating the
memory of this array. This would correspond to placement new, constructing
without allocating. The most obvious way that comes to mind is to merely
explicitly invoke the destructor. This capability is not available on a circa
1990 compiler. Does anyone have any good ideas?


Jul 22 '05 #1
11 5013

"Peter Olcott" <ol****@worldnet.att.net> wrote in message
news:OF*********************@bgtnsc05-news.ops.worldnet.att.net...
I have just built a class that provides the most useful subset of std::vector functionality for use by compilers that lack template capability.

http://home.att.net/~olcott/std_vect.html

Through suggestions from this news group I was able to exactly duplicate the interface of std::vector, except for one aspect.
So you got placement new to work? Congratulations!
What I need is a way
to invoke the destructor on elements of an array without de-allocating the memory of this array. This would correspond to placement new, constructing without allocating. The most obvious way that comes to mind is to merely explicitly invoke the destructor.
That's the way to do it.
This capability is not available on a circa
1990 compiler. Does anyone have any good ideas?


If you don't mind being intrusive, you can require that each
user-defined type provide a destroy function. Otherwise, I don't know.

What syntax are you using for the destructor invocation?

Jonathan

Jul 22 '05 #2
Peter Olcott wrote in
news:OF*********************@bgtnsc05-news.ops.worldnet.att.net:
I have just built a class that provides the most useful subset of
std::vector functionality for use by compilers that lack template
capability.

http://home.att.net/~olcott/std_vect.html

Through suggestions from this news group I was able to exactly
duplicate the interface of std::vector, except for one aspect. What I
need is a way to invoke the destructor on elements of an array without
de-allocating the memory of this array. This would correspond to
placement new, constructing without allocating. The most obvious way
that comes to mind is to merely explicitly invoke the destructor. This
capability is not available on a circa 1990 compiler. Does anyone have
any good ideas?


Assuming that you can't do:

template < typename T >
inline void destroy( T *t )
{
t->~T();
}

Then you could try:

#define OFFSETOFF( Class, Member ) \
(((char *)&((Class *)0)->Member) - ((char *)0))

template < class T >
struct cheat_detor
{
T item;

inline void operator delete (void *) {}

static void destroy( T *ptr )
{
delete (
(cheat_detor< T > *)
(((char *)ptr) - OFFSETOFF( cheat_detor< T >, item ))
);
}
};

template < class T >
inline void destroy( T * ptr )
{
cheat_detor< T >::destroy( ptr );
}

The above is non-standard code, infact g++ will issue a warning
if T is a non-POD type say:

struct X
{
X() {}
~X() {}
};

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
> So you got placement new to work? Congratulations!
Many people suggested placement new, tom_usenet not
only showed me how to make it work on modern compilers,
but also provided the required code to make it work on my
antique compiler.
If you don't mind being intrusive, you can require that each
user-defined type provide a destroy function. Otherwise, I don't know. This is how I got it to work. Before placement new, I also had to have
two Construct functions: Construct() and Construct(const &)
What syntax are you using for the destructor invocation? The currently required member function is merely:
Destruct(); // This invokes the body of the user defined destructor.
The easiest way to do this is to provide the body of the destructor
within Destruct(), and then have ~UserType() { Destruct(); };
Jonathan

Jul 22 '05 #4
> #define OFFSETOFF( Class, Member ) \
(((char *)&((Class *)0)->Member) - ((char *)0))


I think that this seems to be some sort of direct invocation
of the destructor. I can't directly invoke destructors, nor
do I have any template cabability at all. This is for a 1990
C++ compiler that does not have: templates, exceptions,
namespace, direct destructor calls, a delete[]() operator.
Jul 22 '05 #5

"Peter Olcott" <ol****@worldnet.att.net> wrote in message
news:dA*********************@bgtnsc04-news.ops.worldnet.att.net...
What syntax are you using for the destructor invocation? The currently required member function is merely:
Destruct(); // This invokes the body of the user defined

destructor. The easiest way to do this is to provide the body of the destructor
within Destruct(), and then have ~UserType() { Destruct(); };


I meant, what syntax are you using to invoke the destructor
explicitly?

Jonathan
Jul 22 '05 #6
Peter Olcott wrote:
I have just built a class that provides the most useful subset of std::vector
functionality for use by compilers that lack template capability.

http://home.att.net/~olcott/std_vect.html

Through suggestions from this news group I was able to exactly duplicate
the interface of std::vector, except for one aspect. What I need is a way
to invoke the destructor on elements of an array without de-allocating the
memory of this array.


I am confused by the last sentence. AFAICT, std::vector does not invoke
the destructor function on its elements. Although the STL documentation
says, in the case of std::vector::erase for example,
"a.erase(p)...[d]estroys the element pointed to by p and removes it from
a," it is unclear to me that *p's destructor is ever executed.
Certainly, I have not determined this to be the case through simple tests.

/david

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

Jul 22 '05 #7
> > Through suggestions from this news group I was able to exactly duplicate
the interface of std::vector, except for one aspect. What I need is a way
to invoke the destructor on elements of an array without de-allocating the
memory of this array.


I am confused by the last sentence. AFAICT, std::vector does not invoke
the destructor function on its elements. Although the STL documentation
says, in the case of std::vector::erase for example,


I am not speaking from the point of view of the user of this class.
I am speaking from the point of view of the implementor of this
class. Regradless of what the user documentation says, std::vector
must call the destructor on elements of an array, when it moves
elements from one array to another, upon memory re-allocation.
Jul 22 '05 #8
> I meant, what syntax are you using to invoke the destructor
explicitly?

Jonathan

It turns out that the direct destructor call is possible in Borland Turbo C++
1.0, yet it requires non-standard sytax...

Array[N].UserType::~UserType(); // works correctly.
Array[N].~UserType() // is supposed to work, yet doen't


Jul 22 '05 #9
"Peter Olcott" <ol****@worldnet.att.net> wrote in message
news:Rj*********************@bgtnsc05-news.ops.worldnet.att.net...
I meant, what syntax are you using to invoke the destructor
explicitly?

Jonathan
It turns out that the direct destructor call is possible in Borland

Turbo C++ 1.0, yet it requires non-standard sytax...

Array[N].UserType::~UserType(); // works correctly.
Array[N].~UserType() // is supposed to work, yet

doen't

Good. You're all set now!

Jonathan
Jul 22 '05 #10
David Rubin wrote:
Peter Olcott wrote:
I have just built a class that provides the most useful subset of
std::vector
functionality for use by compilers that lack template capability.

http://home.att.net/~olcott/std_vect.html

Through suggestions from this news group I was able to exactly duplicate
the interface of std::vector, except for one aspect. What I need is a way
to invoke the destructor on elements of an array without de-allocating
the
memory of this array.

I am confused by the last sentence. AFAICT, std::vector does not invoke
the destructor function on its elements. Although the STL documentation
says, in the case of std::vector::erase for example,
"a.erase(p)...[d]estroys the element pointed to by p and removes it from
a," it is unclear to me that *p's destructor is ever executed.
Certainly, I have not determined this to be the case through simple tests.

/david


David, your tests are not correct. Try this:

#include <vector>
#include <iostream>

struct X { ~X () { std::cout << "An X is being destroyed.\n"; } };

int main ()
{
{
std::vector <X> v (3);
std::cout << "The vector has been constructed.\n";
a.erase (a.begin ());
std::cout << "One element has just been erased.\n";
}
std::cout << "The vector has been destroyed.\n";
}

It is important that when objects of a class which has a destructor are
destroyed the destructor is called.

Regards,
Buster
Jul 22 '05 #11
Buster wrote:

[snip]
[...] AFAICT, std::vector does not
invoke the destructor function on its elements. Although the STL
documentation says, in the case of std::vector::erase for example,
"a.erase(p)...[d]estroys the element pointed to by p and removes it
from a," it is unclear to me that *p's destructor is ever executed.
Certainly, I have not determined this to be the case through simple
tests.

/david


David, your tests are not correct. Try this:

#include <vector>
#include <iostream>

struct X { ~X () { std::cout << "An X is being destroyed.\n"; } };

int main ()
{
{
std::vector <X> v (3);
std::cout << "The vector has been constructed.\n";
a.erase (a.begin ());
std::cout << "One element has just been erased.\n";
}
std::cout << "The vector has been destroyed.\n";
}

It is important that when objects of a class which has a destructor are
destroyed the destructor is called.


I was confused about the difference between calling the destructor and
deleting the object. For example, v.erase(v.begin()) does call the
destructor for, e.g., the v[0] object; I did see this in my test.
However, replacing the above with

std::vector<X*> v(1);
v[0] = new X;

the call

v.erase(v.begin());

does not 'delete' v[0].

/david

--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."

Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
4
by: Shea Martin | last post by:
Which of the following do I use delete instead of just delete. //1.) // not sure about this one, as char is of size 1 char *str = new char; //2.) //not sure about this one, as it is a...
2
by: skscpp | last post by:
I have a question about multiple inheritance, operator new/delete and ambiguity. Here is some code that I wrote to analyze this problem. // test.h ========================== #include <new>...
8
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:
15
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
6
by: Jay Nabonne | last post by:
Hi, This might sound odd, but we want to replace the allocation scheme used by new and delete without changing operator new and operator delete. (The global operators are shared and we can't...
7
by: morz | last post by:
i just search for a while in c++ groups but cannot find good answer. i have code like this: int **ptr; ptr = new char *; ptr = new int(5); ptr = new int(16);
9
by: bob | last post by:
Let's say you use the delete operator as follows: Rocket *rocket = new Rocket; void *voidptr = (void *) rocket; delete voidptr; Does the memory get deleted right? Do delete operations on...
11
by: asimorio | last post by:
Hi all, If I don't new up a pointer, can I delete it? see code below: void A::foo() { char* buffer; delete buffer; return; }
4
imarkdesigns
by: imarkdesigns | last post by:
Hello everyone.. a newbie programmer here wants to ask why is my codes for deleting rows from my databse won't work.... seems that this codes spread to the net and no one refuse to correct it... ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
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,...
0
jinu1996
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.