Construction of Object c++

R

rishi.shah

I wanted to know how the compiler creates and allocates memory for an
object in C++. When I say
Foo obj
where Foo is a class does the memory for the attributes of class Foo
get allocated on the stack before the default constructor is called or
is memory allocation on the stack done by the constructor? Is object
creation a two step process(allocation of memory on stack followed by
calling the constructor)?
 
A

Alf P. Steinbach

* (e-mail address removed):
I wanted to know how the compiler creates and allocates memory for an
object in C++. When I say
Foo obj

Missing semicolon.

where Foo is a class does the memory for the attributes of class Foo
get allocated on the stack before the default constructor is called or
is memory allocation on the stack done by the constructor?

C++ as a language has no notion of "stack" memory.

In practice, if 'obj' is a local variable, then its memory is allocated
and deallocated using a stack scheme, usually one supported by the
processor.

If 'obj' is a declared outside any function or class, its memory is in
practice allocated as part of the program loading, and deallocated when
the program exits.

If 'obj' is part of some class type object then its memory is in
practice a part of the memory allocated for that containing object.

Is object
creation a two step process(allocation of memory on stack followed by
calling the constructor)?

Allocation + construction, yes, but not necessarily involving a stack.
 
A

Alan Johnson

I wanted to know how the compiler creates and allocates memory for an
object in C++. When I say
Foo obj
where Foo is a class does the memory for the attributes of class Foo
get allocated on the stack before the default constructor is called or
is memory allocation on the stack done by the constructor? Is object
creation a two step process(allocation of memory on stack followed by
calling the constructor)?

How this is done is largely depends on the implementation. The C++
language doesn't specify where or how the memory is allocated, but only
the lifetime of the object (see Automatic storage duration).

A popular implementation is to allocate memory for all local variables
when first entering a function (by adjusting a stack pointer).
Constructors are then called at the appropriate times when objects'
lifetimes begin. This is by no means the only way (or a required way)
to implement automatic storage.
 
I

Ian Collins

I wanted to know how the compiler creates and allocates memory for an
object in C++. When I say
Foo obj
where Foo is a class does the memory for the attributes of class Foo
get allocated on the stack before the default constructor is called or
is memory allocation on the stack done by the constructor? Is object
creation a two step process(allocation of memory on stack followed by
calling the constructor)?
Assuming a stack, think about it this way: stack can't be allocated in
the conventional sense, space for the object is reserved on the stack.
So the memory for the object has to be available before the constructor
is called. The default constructor is called with a single parameter,
the object's 'this' pointer pointing to the reserved bit of the stack.

For dynamic allocation, 'this' points to a newly allocated block of memory.
 
R

rishi.shah

Thanks for reply. I was not aware of the two step object creation
previously.

Regards,
Rishi.
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top