473,397 Members | 2,077 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,397 software developers and data experts.

is 'operator delete' a function?

On comp.lang.c++.moderated

http://groups.google.co.uk/group/com...11da7760?hl=en

the following question was posted

-------------------------------------------------------------------------------------------------------
Is this standard conformant to type cast ::opearator delete to a
function of type (void (*)(void*));
Is not operator delete already a function of this type?
Is the following code conformant?
class A { /* ... */ };
vector<A*v;
v.push_back(new A());
// ...
for_each(v.begin();v.end();(void (*)(void*) ::operator delete);

-------------------------------------------------------------------------------------------------------

Is the following a reasonable solution .
#include<iostream>
#include<memory>
#include<vector>

using namespace std;

struct Base {
virtual ~Base(){}
};

class Derived :public Base {
public:
Derived(){
Count++;
}
Derived(const Derived&){
Count++;
}

~Derived(){Count--;}
static unsigned long Count;
};

unsigned long Derived::Count(0);

template<class T>
struct auto_pimpl {
auto_pimpl(){}
auto_pimpl(T* ptr):theInnerPtr(ptr){}
auto_pimpl(const auto_pimpl<T& theOther):
theInnerPtr(theOther.theInnerPtr.release()){};

auto_pimpl & operator=(const auto_pimpl &theRHS) {
theInnerPtr.reset();
theInnerPtr = auto_ptr<T>(theRHS.theInnerPtr.release());
return *this;
}

T& operator*(void)const{return *theInnerPtr.get();}
T* operator->()const{return theInnerPtr.get();}

private:
mutable auto_ptr<TtheInnerPtr;
};
int main() {

cout << "There are " << Derived::Count
<< " Derived Objects when we start\n";
{
vector<auto_pimpl<Base theVector;
for (unsigned long i(0);i<10;++i){
theVector.push_back(new Derived);

}
cout << "Now there are " << Derived::Count
<< " Derived Objects \n";
}

cout << "Finaly there are " << Derived::Count
<< " Derived Objects \n";
}

I am not subscribed there yet .. so couldn't post there.

Nov 13 '06 #1
3 1627
* Nindi:
On comp.lang.c++.moderated
the following question was posted
[...]
I am not subscribed there yet .. so couldn't post there.
You do not have to "subscribe" to Usenet groups in order to post in
them, although particular client software may make it seem that way.

However, currently, as of 13th November 2006, since sometime yesterday,
the clc++m moderation server is down, which means that postings will be
delayed or worse. Depending on the mail server used to post, how long
this problem persists, and what the problem really is, postings to
clc++m may simply disappear.

So, wait until you see the first new posting appear in clc++m, then try
to post an on-topic message there, e.g. via Google groups.
Hope this helps,

- Alf (moderator clc++m, but a /long/ way away from that server!)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 13 '06 #2
Nindi wrote:
On comp.lang.c++.moderated

http://groups.google.co.uk/group/com...11da7760?hl=en

the following question was posted

-------------------------------------------------------------------------------------------------------
Is this standard conformant to type cast ::opearator delete to a
function of type (void (*)(void*));
Is not operator delete already a function of this type?
Is the following code conformant?
class A { /* ... */ };
vector<A*v;
v.push_back(new A());
// ...
for_each(v.begin();v.end();(void (*)(void*) ::operator delete);
It is only memory freeing. Destructors of objects doesn't invoke.
Try
class delete_object
{
template <typename T>
void operator()(T* p)
{
delete p;
}
};
....
for_each(v.begin(),v.end(),delete_object<T>);
-------------------------------------------------------------------------------------------------------

Is the following a reasonable solution .
#include<iostream>
#include<memory>
#include<vector>

using namespace std;

struct Base {
virtual ~Base(){}
};

class Derived :public Base {
public:
Derived(){
Count++;
}
Derived(const Derived&){
Count++;
}

~Derived(){Count--;}
static unsigned long Count;
};

unsigned long Derived::Count(0);

template<class T>
struct auto_pimpl {
auto_pimpl(){}
auto_pimpl(T* ptr):theInnerPtr(ptr){}
auto_pimpl(const auto_pimpl<T& theOther):
theInnerPtr(theOther.theInnerPtr.release()){};

auto_pimpl & operator=(const auto_pimpl &theRHS) {
theInnerPtr.reset();
theInnerPtr = auto_ptr<T>(theRHS.theInnerPtr.release());
return *this;
}

T& operator*(void)const{return *theInnerPtr.get();}
T* operator->()const{return theInnerPtr.get();}

private:
mutable auto_ptr<TtheInnerPtr;
};
int main() {

cout << "There are " << Derived::Count
<< " Derived Objects when we start\n";
{
vector<auto_pimpl<Base theVector;
for (unsigned long i(0);i<10;++i){
theVector.push_back(new Derived);

}
cout << "Now there are " << Derived::Count
<< " Derived Objects \n";
}

cout << "Finaly there are " << Derived::Count
<< " Derived Objects \n";
}

I am not subscribed there yet .. so couldn't post there.
Nov 13 '06 #3

Shelyag Yuriy wrote:
Nindi wrote:
On comp.lang.c++.moderated

http://groups.google.co.uk/group/com...11da7760?hl=en

the following question was posted

-------------------------------------------------------------------------------------------------------
Is this standard conformant to type cast ::opearator delete to a
function of type (void (*)(void*));
Is not operator delete already a function of this type?
Is the following code conformant?
class A { /* ... */ };
vector<A*v;
v.push_back(new A());
// ...
for_each(v.begin();v.end();(void (*)(void*) ::operator delete);
It is only memory freeing. Destructors of objects doesn't invoke.
Try
class delete_object
{
template <typename T>
void operator()(T* p)
{
delete p;
}
};
...
for_each(v.begin(),v.end(),delete_object<T>);
-------------------------------------------------------------------------------------------------------

Is the following a reasonable solution .
#include<iostream>
#include<memory>
#include<vector>

using namespace std;

struct Base {
virtual ~Base(){}
};

class Derived :public Base {
public:
Derived(){
Count++;
}
Derived(const Derived&){
Count++;
}

~Derived(){Count--;}
static unsigned long Count;
};

unsigned long Derived::Count(0);

template<class T>
struct auto_pimpl {
auto_pimpl(){}
auto_pimpl(T* ptr):theInnerPtr(ptr){}
auto_pimpl(const auto_pimpl<T& theOther):
theInnerPtr(theOther.theInnerPtr.release()){};

auto_pimpl & operator=(const auto_pimpl &theRHS) {
theInnerPtr.reset();
theInnerPtr = auto_ptr<T>(theRHS.theInnerPtr.release());
return *this;
}

T& operator*(void)const{return *theInnerPtr.get();}
T* operator->()const{return theInnerPtr.get();}

private:
mutable auto_ptr<TtheInnerPtr;
};
int main() {

cout << "There are " << Derived::Count
<< " Derived Objects when we start\n";
{
vector<auto_pimpl<Base theVector;
for (unsigned long i(0);i<10;++i){
theVector.push_back(new Derived);

}
cout << "Now there are " << Derived::Count
<< " Derived Objects \n";
}

cout << "Finaly there are " << Derived::Count
<< " Derived Objects \n";
}

I am not subscribed there yet .. so couldn't post there.

But what if there is a throw between populating the vector and the
for_each statement.. surly it is better to use a smart pointer .

Nov 13 '06 #4

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

Similar topics

24
by: Marcin Vorbrodt | last post by:
Is there any reason why auto_ptr does not have the cast operator like this: operator void* (); So that one could easily test auto_ptr for NULL ??? Standard does not declare such operator. Is...
0
by: Tim Milstead | last post by:
(CODE AT END OF POST) I have got some code for finding memory leaks that works well with: new() and needs to be extended to work with new
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
2
by: Shark | last post by:
Hi, if we need to change the behavior of operator new, it is called overriding or overloading? My other question is, if we change the behavior of operator new, do we use malloc to do that or we use...
5
by: tom | last post by:
Hi, I'm overriding my operator new and operator delete for two classes, one inherited from the other, so I can use my own memory pool. A simplified version of what I have is below: class...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
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
0
BarryA
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...
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
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...
0
Oralloy
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,...
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...
0
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...
0
agi2029
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,...

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.