if Constructor can be used to initialize ...

2

2005

If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?

Thanks
 
A

Alf P. Steinbach

* 2005:
If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?

Sometime before the constructor is called.

For the "new" operator that's immediately before.
 
S

Salt_Peter

2005 said:
If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?

Thanks

You don't need the new operator to allocate - in a perfect world, using
new should be discouraged.
A ctor's primary purpose is to allocate an instance based on the
contents of its type (ignoring inherited classes for now) and whatever
resources are required.
In C++ it happens to have a secondary purpose that is to initialize
members, if any, using an init list. The initialisation is not a
requirement but too often overlooked and quite usefull.

To answer your question: allocation happens when the constructor is
invoked.
and "invoke" does not mean "call".

#include <iostream>

class N
{
int n;
public:
N() : n(0) { std::cout << "invoke ctor\n"; }
~N() { std::cout << "invoke d~tor\n"; }
const int& get() const { return n; }
};

int main()
{
N instance; // allocation happens here
std::cout << "instance.n = " << instance.get() << std::endl;

return 0;
} // deallocation happens here - at end of scope

/*
invoke ctor
instance.n = 0
invoke d~tor
*/

The explanation is in the FAQ:
http://www.parashift.com/c++-faq-lite/ctors.html
 
A

Alf P. Steinbach

* Salt_Peter:
You don't need the new operator to allocate - in a perfect world, using
new should be discouraged.

Sorry, that's meaningless.

A ctor's primary purpose is to allocate an instance based on the
contents of its type (ignoring inherited classes for now) and whatever
resources are required.

Sorry, that's incorrect.

In C++ it happens to have a secondary purpose that is to initialize
members, if any, using an init list.

Sorry, that's incorrect.

The initialisation is not a
requirement but too often overlooked and quite usefull.

Sorry, that's incorrect.


To answer your question: allocation happens when the constructor is
invoked.

Sorry, that's incorrect.

and "invoke" does not mean "call".

Sorry, that's incorrect.


#include <iostream>

class N
{
int n;
public:
N() : n(0) { std::cout << "invoke ctor\n"; }
~N() { std::cout << "invoke d~tor\n"; }
const int& get() const { return n; }
};

int main()
{
N instance; // allocation happens here

Sorry, that's incorrect (although in practice true).

std::cout << "instance.n = " << instance.get() << std::endl;

return 0;
} // deallocation happens here - at end of scope

Sorry, that's incorrect (although in practice true).

/*
invoke ctor
instance.n = 0
invoke d~tor
*/

The explanation is in the FAQ:
http://www.parashift.com/c++-faq-lite/ctors.html

Sorry, that's incorrect (this FAQ section does not concern allocation,
and provides no explanation of when allocation occurs).
 
M

mimi

Anyway, I think the faq-list is exactly what 2005 needs.
Alf P. Steinbach 写é“:
 
K

Kai-Uwe Bux

Please do not top-post in this group. It's frowned upon.

2005 wrote [top-posting and sig-quoting corrected]
Does it mean that if we use a constructor, we don't need a "new"
operator?

An object is a region of storage [1.8/1]. Thus, before you initialize an
object, you need to have that region of memory. Now, how you get that
region of storage, depends on how the object is created (not initialized).

An object is created by a definition, by a new-expression, or by the
implementation when needed [1.8/1]. Examples:

a) Defining a variable:

SomeClass some_var ( some_args );

Memory is allocated automagically by the compiler. Then the object is
created.


b) Dynamic allocation via new:

SomeClass* some_ptr = new SomeClass ( some_args );

Here, new will allocate memory by calling an allocation function. Then the
constructor is called to initialize the object.


c) Creating a temporary:

some_function( SomeClass( some_args ) );

Memory allocation happens without you doing anything.


Best

Kai-Uwe Bux
 
2

2005

Kai-Uwe Bux said:
Please do not top-post in this group. It's frowned upon.

2005 wrote [top-posting and sig-quoting corrected]
Sorry about it - will follow it in the future.

Thanks for letting me know.
 
A

Alf P. Steinbach

* 2005:
You say "Sorry, that's incorrect." - Could you pls give explanations?

Yes, if you'd care to respect the conventions of the group¹: don't
top-post, don't quote signatures, quote only relevant things, but do
quote the relevant things, ask specific questions (about what you quoted
right above the specific questions), and use plain English.

Otherwise this could amount to a fullblown tutorial on basic C++.

And I've already written that² -- and besides, there isn't room enough
in a Usenet article.


Notes:
¹ In an earlier article in this thread, in reply to Kai-Uwe Bux' request
for the same, you wrote «Sorry about it - will follow it in the
future.», but you didn't.
² See the FAQ item titled «What other "newbie" guides are there for
me?», currently at <url:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.21>.
 
2

2005

Alf said:
* 2005:

Yes, if you'd care to respect the conventions of the group¹: don't
top-post, don't quote signatures, quote only relevant things, but do
quote the relevant things, ask specific questions (about what you quoted
right above the specific questions), and use plain English.

Otherwise this could amount to a fullblown tutorial on basic C++.

And I've already written that² -- and besides, there isn't room enough
in a Usenet article.


Notes:
¹ In an earlier article in this thread, in reply to Kai-Uwe Bux' request
for the same, you wrote «Sorry about it - will follow it in the
future.», but you didn't.

Not True; Pls look at the time stamp.
I replied you before I replied to Kai-Uwe Bux' request - makes sense
 
2

2005

Alf said:
And I've already written that² -- and besides, there isn't room enough
in a Usenet article.


Notes:
¹ In an earlier article in this thread, in reply to Kai-Uwe Bux' request
for the same, you wrote «Sorry about it - will follow it in the
future.», but you didn't.
² See the FAQ item titled «What other "newbie" guides are there for
me?», currently at <url:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.21>.

Are you the author of "www.parashift.com" or
"www.parashift.com/c++-faq-lite/newbie.html#faq-29.21"?

Thanks
 
A

Alf P. Steinbach

* 2005:
Not True; Pls look at the time stamp.
I replied you before I replied to Kai-Uwe Bux' request - makes sense

Ah, well, sorry, I'll do a summary: look elsethread.
 
A

Alf P. Steinbach

* 2005:
You say "Sorry, that's incorrect." - Could you pls give explanations?

In C++ constructors don't allocate memory for the object they're
constructing (they can allocate memory for members). The main purpose
of a C++ constructor is to couple initializion to object creation, so
that you can't have one without the other ("creation": that the object
becomes available for use). A simple static local variable is an
example where allocation in practice happens way long before the object
is created: the object is created the first time execution passes the
declaration, the memory for the object is reserved at program startup.


See above.


See above.


See above.


In the standard and common usage the terms "invoke" and "call" are used
interchangeably.


Formally the compiler is allowed to allocate memory for N any time
before the object is created ("created": becomes available for usage).


See above.


As said.
 

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

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top