Construction of Object c++

Discussion in 'C++' started by rishi.shah@patni.com, Aug 12, 2006.

  1. Guest

    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)?
    , Aug 12, 2006
    #1
    1. Advertising

  2. * :
    > 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: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 12, 2006
    #2
    1. Advertising

  3. Alan Johnson Guest

    wrote:
    > 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.

    --
    Alan Johnson
    Alan Johnson, Aug 12, 2006
    #3
  4. Ian Collins Guest

    wrote:
    > 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.

    --
    Ian Collins.
    Ian Collins, Aug 12, 2006
    #4
  5. Guest

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

    Regards,
    Rishi.

    Ian Collins wrote:
    > wrote:
    > > 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.
    >
    > --
    > Ian Collins.
    , Aug 12, 2006
    #5
  6. Ian Collins Guest

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

    No problem, but please don't top-post in future.

    --
    Ian Collins.
    Ian Collins, Aug 12, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anthony
    Replies:
    1
    Views:
    295
    Victor Bazarov
    Sep 23, 2003
  2. Jerivix Entadi
    Replies:
    4
    Views:
    371
    John Harrison
    Feb 23, 2004
  3. newbiecpp

    Object construction

    newbiecpp, Jul 22, 2004, in forum: C++
    Replies:
    2
    Views:
    425
    SaltPeter
    Jul 22, 2004
  4. Ook
    Replies:
    10
    Views:
    540
  5. cantide5ga
    Replies:
    7
    Views:
    378
    cantide5ga
    Oct 23, 2007
Loading...

Share This Page