By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 912 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Constructor

P: n/a
Hi,

Does a compiler generated constructor initialise the data members?

Giles
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
giles writes:
Does a compiler generated constructor initialise the data members?


No, the data elements will have whatever random bits they had from some
previous use.
Jul 19 '05 #2

P: n/a

giles <gi***************@yahoo.co.in> wrote in message
news:2a**************************@posting.google.c om...
Hi,

Does a compiler generated constructor initialise the data members?


Only those which have default constructors. This of
course does not include the built-in types.

So, the answer depends upon the data member(s) type(s).

-Mike

Jul 19 '05 #3

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<bg**********@slb6.atl.mindspring.net>...
giles <gi***************@yahoo.co.in> wrote in message
news:2a**************************@posting.google.c om...
Hi,

Does a compiler generated constructor initialise the data members?


Only those which have default constructors. This of
course does not include the built-in types.

So, the answer depends upon the data member(s) type(s).

-Mike


Have another query...
Contructor is used only to initialise the object and not to create it
in the memory isn't it?
When a contructor throws an exception the destructor for it is not
called. Why is this? The object is created in the memory, its just
that its not initialised so why shouldn't the destructor get called
and how does the compiler know it shouldn't call the destructor?

Giles
Jul 19 '05 #4

P: n/a
giles wrote in news:2a**************************@posting.google.c om:
Have another query...
Contructor is used only to initialise the object and not to create it
in the memory isn't it?
Assuming you mean "allocating the memory for the object" when you say
"create it in the memory" then yes.
When a contructor throws an exception the destructor for it is not
called. Why is this? The object is created in the memory, its just
that its not initialised so why shouldn't the destructor get called
The answer is in you question, like the constructor isn't responsible
for "creating it in the memory". The destructor isn't responsable for
deallocating the object's memory. All it does is uninitialise it,
since the constructor didn't complete the object's memory is still
uninitialised so the destructor call isn't required.

Note that when you write:

X *x = new X;

and X's constructor throw's, all though the compiler doesn't have to
arrange for X's destructor to be called it does have to arrange for
operator delete( x ); to be called, to deallocate the objects memory.
and how does the compiler know it shouldn't call the destructor?


Because its a computer programme and its been written that way (*).

(*) I'm just guessing :).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #5

P: n/a
giles <gi***************@yahoo.co.in> wrote in message
news:2a**************************@posting.google.c om...
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<bg**********@slb6.atl.mindspring.net>...
giles <gi***************@yahoo.co.in> wrote in message
news:2a**************************@posting.google.c om...
Hi,

Does a compiler generated constructor initialise the data members?


Only those which have default constructors. This of
course does not include the built-in types.

So, the answer depends upon the data member(s) type(s).

-Mike


Have another query...
Contructor is used only to initialise the object and not to create it
in the memory isn't it?


That's the same thing. Before the ctor is invoked, memory for
the object is alloted (either as a static or automatic object, or
dynamically allocated). Then the constructor is called, which
'constructs' (a.k.a. initializes) the object.
When a contructor throws an exception the destructor for it is not
called. Why is this?
Because if the constructor does not complete, then the
object has not been constructed. You don't need a destructor
called for an object which hasn't been created.

However, if you allocate your object with operator 'new'
and the ctor throws, then the compiler is responsible for
ensuring that the allocated memory is freed. Note that
there's still no destructor call. A destructor is only
for 'cleaning up' a fully constructed object. An object
whose contstructor has thrown an exception is not such
an object.
The object is created in the memory,
Memory space is made for the object when it is allocated
or defined. No real 'object' exists yet at that point,
only 'raw' memory. It's the constructor that actually
'builds' ('creates') the object in that memory, i.e.
initializes the data members.
its just
that its not initialised
Right. There's no 'object' yet, only 'raw' memory.
so why shouldn't the destructor get called
Because there's nothing to destruct. :-)
and how does the compiler know it shouldn't call the destructor?


The language definition dicates that. A compiler that
conforms to the language will 'know'.

-Mike

Jul 19 '05 #6

P: n/a
Note that when you write:

X *x = new X;

and X's constructor throw's, all though the compiler doesn't have to
arrange for X's destructor to be called it does have to arrange for
operator delete( x ); to be called, to deallocate the objects memory.
> and how does the compiler know it shouldn't call the destructor?


I am not sure when an object is created using 'new', compiler would
arrange for the memory to be de-allocated. It should be a memory leak.


That is the way the language works. If the ctor throws, no matter if
it was allocated with new or not, no memory is allocated.
Jonathan

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.