473,748 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comprehensive treatment of new / delete

Hello all,

I'd like to find a source on the web that discusses, in a comprehensive
manner and in one place, everything about new / delete. It should include
overloading operator new, the new operator, placement, nothrow, arrays,
etc...

My books cover the topic, I've found FAQs on the web that cover the topic,
and so on, but all the sources I've found are disjointed. There's a bit on
this page, a bit on that page, and so on. The information may all be there,
but it's kind of hard to piece it all together.

If anybody knows of an article out there that gives a comprehensive
treatment of this topic in one place, I'd appreciate a link.

Thanks,
Dave
Jul 23 '05 #1
2 2085
Dave wrote:
Hello all,

I'd like to find a source on the web that discusses, in a comprehensive
manner and in one place, everything about new / delete. It should include
overloading operator new, the new operator, placement, nothrow, arrays,
etc...

My books cover the topic, I've found FAQs on the web that cover the topic,
and so on, but all the sources I've found are disjointed. There's a bit on
this page, a bit on that page, and so on. The information may all be there,
but it's kind of hard to piece it all together.

If anybody knows of an article out there that gives a comprehensive
treatment of this topic in one place, I'd appreciate a link.


I don't know of one but let's see if we can make one !

http://www.google.com/search?q=new+d....parashift.com

That will give you a fairly detailed treatment of the topic.

The code below covers most of the new/delete usages I have used.

------------------------------------------------------------------------
#include <unistd.h>
#include <cstring>

// non-portable but non-allocating write method
void Write( const char * l_str )
{
::write( 1, l_str, std::strlen( l_str ) );
::write( 1, "\n", 1 );
}

#include <new>
#include <cstdlib>
inline void * operator new ( size_t i_val )
{
Write( "new called" );

void * l_ptr = std::malloc( i_val );
return l_ptr;
}

inline void operator delete ( void * i_ptr )
{
Write( "delete called" );

if ( i_ptr )
{
std::free( i_ptr );
}
}

struct A
{
A()
{
Write( "A contructed" );
}

~A()
{
Write( "A destructed" );
}
};

template <typename X>
void test1()
{

X * l_a = new X;

delete l_a;

X * l_b = new X[3];

delete [] l_b;
}
// This does not seem to be called - no matter what
inline void operator delete ( void * i_ptr, size_t i_val )
{
Write( "delete (with size) called" );

if ( i_ptr )
{
std::free( i_ptr );
}
}
template <typename X>
void test2()
{

X * l_a = new X;

delete l_a;

X * l_b = new X[3];

delete [] l_b;
}
struct B
{

inline void * operator new ( size_t i_val )
{
Write( "B::new called" );

void * l_ptr = std::malloc( i_val );
return l_ptr;
}
inline void operator delete ( void * i_ptr )
{
Write( "B::delete (with size) called" );

if ( i_ptr )
{
std::free( i_ptr );
}
}

B()
{
Write( "B contructed" );
}

~B()
{
Write( "B destructed" );
}
};

struct C
{

inline void * operator new ( size_t i_val )
{
Write( "C::new called" );

void * l_ptr = std::malloc( i_val );
return l_ptr;
}
inline void * operator new ( size_t i_val, void * i_ptr )
{
Write( "C::new placement new called" );

return i_ptr;
}
inline void operator delete ( void * i_ptr, size_t i_val )
{
Write( "C::delete (with size) called" );

if ( i_ptr )
{
std::free( i_ptr );
}
}

C()
{
Write( "C contructed" );
}

~C()
{
Write( "C destructed" );
}
};

template <typename X>
void test3()
{

long x[ ( sizeof( long ) + sizeof( X ) -1 )/sizeof( long ) ];

X * l_x = new ( ( void * ) x ) X;

l_x->~X();

}

int main()
{
Write( "\ntest1<A> " );
test1<A>();

Write( "\ntest2<A> " );
test2<A>();

Write( "\ntest1<B> " );
test1<B>();

Write( "\ntest1<C> " );
test1<C>();

Write( "\ntest3<A> " );
test3<A>();

Write( "\ntest3<C> " );
test3<C>();

}

-------- output ----------

../new_del

test1<A>
new called
A contructed
A destructed
delete called
new called
A contructed
A contructed
A contructed
A destructed
A destructed
A destructed
delete called

test2<A>
new called
A contructed
A destructed
delete called
new called
A contructed
A contructed
A contructed
A destructed
A destructed
A destructed
delete called

test1<B>
B::new called
B contructed
B destructed
B::delete (with size) called
new called
B contructed
B contructed
B contructed
B destructed
B destructed
B destructed
delete called

test1<C>
C::new called
C contructed
C destructed
C::delete (with size) called
new called
C contructed
C contructed
C contructed
C destructed
C destructed
C destructed
delete called

test3<A>
A contructed
A destructed

test3<C>
C::new placement new called
C contructed
C destructed
Jul 23 '05 #2
Dave wrote:
Hello all,

I'd like to find a source on the web that discusses, in a comprehensive
manner and in one place, everything about new / delete. It should include
overloading operator new, the new operator, placement, nothrow, arrays,
etc... [snip] If anybody knows of an article out there that gives a comprehensive
treatment of this topic in one place, I'd appreciate a link.

Thanks,
Dave


Some samples with 'new'
http://groups.google.com/group/comp....2c7f9231d4b03b

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Aug 2 '05 #3

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

Similar topics

2
2028
by: Geoffrey | last post by:
Dear Java-lovers, We need a really good charting tool for the users to do all kind of things to the graphs, but we don't have time to develop one, so we decided to purchase an existing one out there. The basic requirements are as follows: * coded in Java * source code can be purchased * includes drawing features (e.g. trend, fibonacci, notes... etc.)
2
1403
by: William Gill | last post by:
I know a major problem I am having is that I am finding lots of Tkinter information in 'fragments' of various , sometimes conflicting vintages. For example the python reference I was using didn't show the '%%' as an escape sequence, I posted asking how to escape the '%' and after several helpful responses voila I found a more complete table of escape sequences. Is there a comprehensive document or book that I can get that is relatively...
30
1969
by: somebody | last post by:
I'm posting a new message regarding this subject, since the one posted previously is getting rather large. I just received the June 2005 issue of Dr. Dobb's Journal, and it contains an article titled "The Technical Report On C++ Library Extensions". After debating this issue with various group members, all I can say is: I Win!!! (Just kidding) Anyway, here are a couple of quotes from the article,
3
2142
by: howard dierking | last post by:
If I understand the Xml Serializer correctly, as it is serializing an object graph, if it comes across a collection object, it puts a tag in for the collection and then tries to serialize all of the members of the collection. This is roughly the same treatment when the top of my object graph is itself a collection. I'm curious, however, about 2 things with regard to passing a custom collection object to the XmlSerializer: 1) Why...
0
1178
by: Thomas F.O'Connell | last post by:
Matthew, Here's some more feedback on our use of pg_autovaccum. It's clear that it's working and that it's helping, but even after increasing our max_fsm_pages substantially (to in excess of what vacuum verbose suggests is needed), we're still seeing pretty a rapid increase in disk usage. It used to be that nightly reindexing helped substantially, but am I wrong in thinking that the frequency of dynamic analysis is helping
1
1567
by: Paradox | last post by:
Hi, new here. I've been looking all over the web for a comprehensive reference guide for javascript. Something like a cheat sheet where it lists all or a heck of a lot of the function names, a very brief description of what they do and the like. It would be a god send to have a list that I can just skim over if I can't remember a method or object off hand. So far, the best thing I've found is w3schools.com
2
1465
by: usenet | last post by:
I am trying to collect a comprehensive listing of standard C++ exceptions. This is what I have got so far (by going through the standard). Kindly let me know if I missed anything. Thanks, Ramesh // // ********* Derived from std::exception ********* //
3
2309
by: Constantine AI | last post by:
Hi can anybody help me with this problem? I have a customer order form with a sales order line subform. The subform contains products ordered by customers, each (wooden) product can come in two different treatment types; golden brown or pressure treated of which are of two different prices. The prices are also determined by the customer type; pub, garden centre or public. Currenlty on the treatment combo box, i have a dlookup in the code...
0
8991
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, 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...
0
8831
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,...
0
9376
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 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...
0
9249
tracyyun
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...
0
8245
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, 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...
1
6796
isladogs
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...
0
6076
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();...
0
4607
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...
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.