new-> constructor-> destructor-> delete

Y

yancheng.cheok

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<< "constructor"<< 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
 
I

IR

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,
 
J

Jim Langston

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<< "constructor"<< 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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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 to be
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top