473,785 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new-> constructor-> destructor-> delete

Hello all, I try to figure out what is the sequence when we create and
delete an object. After experiment on my VC++ 2003, I found that here
is the sequence

new-constructor-destructor-delete

What I am concern is, is this sequence a C++ standard specification? Or
it is compiler dependent?

Thank you. Here is the code I use to perform experiment:

#include <iostream>
#include <cstdlib>

using namespace std;

class a
{
public:
a()
{
cout<< "constructo r"<< endl;
}

~a()
{
cout<< "destructor "<< endl;
}

void * operator new(size_t s)
{
cout<< "new"<< endl;
return malloc(s);
}

void operator delete(void * mem)
{
cout<< "delete"<< endl;
free(mem);
}
};

int main()
{
a* aa = new a();
delete aa;

getchar();
}

Here is the result:

new
constructor
destructor
delete

Dec 13 '06 #1
3 2086
IR
ya************@ gmail.com wrote:
Hello all, I try to figure out what is the sequence when we create
and delete an object. After experiment on my VC++ 2003, I found
that here is the sequence

new-constructor-destructor-delete

What I am concern is, is this sequence a C++ standard
specification? Or it is compiler dependent?
It is a standard requirement. I couldn't quote the relevant part(s),
but I guess one of the gurus around here will.
Cheers,
--
IR
Dec 13 '06 #2
<ya************ @gmail.comwrote in message
news:11******** **************@ 73g2000cwn.goog legroups.com...
Hello all, I try to figure out what is the sequence when we create and
delete an object. After experiment on my VC++ 2003, I found that here
is the sequence

new-constructor-destructor-delete

What I am concern is, is this sequence a C++ standard specification? Or
it is compiler dependent?

Thank you. Here is the code I use to perform experiment:

#include <iostream>
#include <cstdlib>

using namespace std;

class a
{
public:
a()
{
cout<< "constructo r"<< endl;
}

~a()
{
cout<< "destructor "<< endl;
}

void * operator new(size_t s)
{
cout<< "new"<< endl;
return malloc(s);
}

void operator delete(void * mem)
{
cout<< "delete"<< endl;
free(mem);
}
};

int main()
{
a* aa = new a();
delete aa;

getchar();
}

Here is the result:

new
constructor
destructor
delete
AFAIK it's the only way it could work. There has to be memory set aside
before the constructor can be called. So it would have to be new ->
constructor

And the destructor has to have memory to run against, so it would have to be
destructor -delete also.
Dec 14 '06 #3
On Dec 14, 3:32 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
<yancheng.ch... @gmail.comwrote in messagenews:11* *************** ******@73g2000c wn.googlegroups .com...
Hello all, I try to figure out what is the sequence when we create and
delete an object. After experiment on my VC++ 2003, I found that here
is the sequence
new-constructor-destructor-delete
What I am concern is, is this sequence a C++ standard specification? Or
it is compiler dependent?
Thank you. Here is the code I use to perform experiment:
[snip]
>
AFAIK it's the only way it could work. There has to be memory set aside
before the constructor can be called. So it would have to be new ->
constructor

And the destructor has to have memory to run against, so it would have tobe
destructor -delete also.
Notice though that when using a container, like a vector, the new and
delete-parts can be independent of the construction/destruction. The
vector can allocate the memory needed for a number of elements (find
out how many by using capacity() ) and then use placement new to
construct the elements on insertion.

--
Erik Wikström

Dec 14 '06 #4

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

Similar topics

1
1519
by: scorpiotgs | last post by:
I downloaded a script for a menu (below) but I want all the links i this part(ze_menu3) to open in a new window. this probably a noob question, but i couldnt figure it out, so pleas help me "ez_Menu= new Array("cbg.nl~Centraal Bureau voor Genealogie te De Haag^http://www.cbg.nl", "genlias.nl^http://www.genlias.nl" "rijksarchieflimburg.nl^http://www.rijksarchieflimburg.nl" "deschoolbank.nl^http://www.deschoolbank.nl"...
15
4662
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any field to start. If I edit that field and then click on the new record button in the navigation buttons, the form goes to a new record and each field has the default value of the previous record. If I put the focus in any field to start, edit that...
2
1313
by: jm | last post by:
I got the code below from the .NET SDK. I am still new to C# and I don't understand how the code works. And by that I mean I don't understand how the program knows to execute it. I can find no entry point besides main. I cannot find an event that calls the code. It is a simple GDI+ application. I just don't see how it ever executes. I need nothing about the code itself, just how the thing "turns on." I don't ever see OnPaint called...
7
2479
by: Mike Bulava | last post by:
I have created a base form that I plan to use throughout my application let call the form form1. I have Built the project then add another form that inherits from form1, I add a few panel controls each with a couple of controls in them I then rebuilt my project and my new panels and all controls they contained are gone... I've looked through the Auto generated code but don't see anything that looks wrong Any body have any idea why this...
7
2973
by: Sakharam Phapale | last post by:
Hi All, How to preserve the old font properties while changing new one? I posted same question 2 months back, but I had very small time then. eg. "Shopping for" is a text in RichTextBox and already formatted as "Shopping" ---Bold "for" -----Regular Now I want to underline whole text by preserving old style i.e. Bold and
0
1387
by: Ben | last post by:
module main ... application.run(new splashform) .. end module after a few screen, I try to load a new codes I got from MSDN on datagrid that works on its own. I took out submain and ran datagridForm from my mainForm:
12
2611
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for example: 21/05/2006
3
1891
by: Grizlyk | last post by:
Hi, people. Can anybody explain me "multiple 'new' at single line" behavior. Consider: p::p(void*); p::p(void*,void*); new A( p(new B), p( new C(p(new D), p(new E)) ), p(new F));
4
2875
by: rgparkins | last post by:
Hello I am running out of time with a problem I have running PHP 5.04 and Apache 2.0 and really need help :(. I have a page that stores a variable in session but each time I reload that page the session seems to be re-created and is an empty array. I have checked the session file and the variable is being stored against the session id, but I dont know why PHP is not picking up the session after I reload.. I have tried the usual suspects...
3
4274
by: rcoco | last post by:
Hi, I want to share this problem. I have a datagrid that will help me Insert data into sql database. So I made a button On my form so that when I press the button a new row on datagrid should be created and I could be able to insert data. But with this code below I've failed could someone help me and tell me where I'm going wrong: private void Page_Load(object sender, System.EventArgs e) { if (! IsPostBack)
0
9483
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
10346
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, 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...
0
10157
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...
1
10096
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8982
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...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
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
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
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...

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.