468,746 Members | 1,921 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,746 developers. It's quick & easy.

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

Dec 13 '06 #1
3 1851
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.googlegro ups.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<< "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.
Dec 14 '06 #3
On Dec 14, 3:32 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
<yancheng.ch...@gmail.comwrote in messagenews:11**********************@73g2000cwn.go oglegroups.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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by scorpiotgs | last post: by
2 posts views Thread by jm | last post: by
3 posts views Thread by Grizlyk | last post: by
4 posts views Thread by rgparkins | last post: by
3 posts views Thread by rcoco | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.