Connecting Tech Pros Worldwide Forums | Help | Site Map

new-> constructor-> destructor-> delete

yancheng.cheok@gmail.com
Guest
 
Posts: n/a
#1: Dec 13 '06
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


IR
Guest
 
Posts: n/a
#2: Dec 13 '06

re: new-> constructor-> destructor-> delete


yancheng.cheok@gmail.com wrote:
Quote:
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
Jim Langston
Guest
 
Posts: n/a
#3: Dec 14 '06

re: new-> constructor-> destructor-> delete


<yancheng.cheok@gmail.comwrote in message
news:1166037604.056258.257380@73g2000cwn.googlegro ups.com...
Quote:
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?=
Guest
 
Posts: n/a
#4: Dec 14 '06

re: new-> constructor-> destructor-> delete


On Dec 14, 3:32 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
Quote:
<yancheng.ch...@gmail.comwrote in messagenews:1166037604.056258.257380@73g2000cwn.go oglegroups.com...
>
Quote:
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
>
Quote:
new-constructor-destructor-delete
>
Quote:
What I am concern is, is this sequence a C++ standard specification? Or
it is compiler dependent?
>
Quote:
Thank you. Here is the code I use to perform experiment:
>
[snip]
Quote:
>
>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

Closed Thread