Find the answer to your Linux question:
Results 1 to 10 of 10
Hi All, I hve a question C++ class objects -Who will create memory for a class object(compiler or constructor or anyother) -Who will call the constructor when an oject is ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    21

    Memory creation for object in C++


    Hi All,

    I hve a question C++ class objects
    -Who will create memory for a class object(compiler or constructor or anyother)
    -Who will call the constructor when an oject is created.

    --Thanks in advance
    Madhu

  2. #2
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by madhuti View Post
    -Who will create memory for a class object(compiler or constructor or anyother)
    I guess that depends on whether it's created on the stack or on the heap. If you declare an object like this:

    Code:
    Object obj;
    then the compiler will set aside the necessary stack space. If you declare it like this:

    Code:
    Object *obj = new Object;
    Then the C++ standard library will allocate the memory on the heap for you.



    Quote Originally Posted by madhuti View Post
    -Who will call the constructor when an oject is created.
    The compiler will ensure the constructors are always called - the constructor is automatically called for you in both the above examples.

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    21

    re: Memory creation for object in C++

    Thanks for giving reply,
    On both the cases constructor will be called.

    But, my question is who and when will call the constructor be called.
    ex:
    class A{
    public:
    A() {}
    };
    ...
    A a; // Here i want to know, whether 1st memory will be created or constructor will be called.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by madhuti View Post
    A a; // Here i want to know, whether 1st memory will be created or constructor will be called.
    Well, in that specific case the memory will be on the stack, and as such may have been set aside by the compiler before you even reach that statement, or the stack may be extended when you get there.

    But whether you declare it on the stack or on the heap, the memory will be allocated before the constructor is called.

  6. #5
    Just Joined!
    Join Date
    Feb 2006
    Posts
    13
    Constructor is used to initialize the class member variables it will not allocate any memory.

    Even if you don't provide one, default constructor will be generated to initialize the variables.

  7. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,753
    Quote Originally Posted by srinathduraisamy View Post
    Constructor is used to initialize the class member variables it will not allocate any memory.

    Even if you don't provide one, default constructor will be generated to initialize the variables.
    Not precisely true. The constructor (default or otherwise) will call the constructor of member variables that have constructors, but scalar types and pointers will contain whatever is in memory at their location, quite possibly garbage. This is why initializing your member variables to a sane value, especially scalar types and pointers, is as important in C++ programming as it is in a C program.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #7
    Just Joined!
    Join Date
    Feb 2006
    Posts
    13
    Yes I am wrong with initialization part.

    But still the memory for the object is not allocated in constructor, rite.

  9. #8
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,753
    Quote Originally Posted by srinathduraisamy View Post
    Yes I am wrong with initialization part.

    But still the memory for the object is not allocated in constructor, rite.
    True enough. Everything else you said was pretty much spot on. Automatics (on the stack) are given space on the stack and then constructed when the function they are declared in are entered, and their destructors are called and the stack given back to the program when the function exists. Object created with operator new have their memory allocated, the constructor is called on the address returned by the allocator, and the constructed object's pointer is returned to the caller of new. Each class has a default (hidden) static function new that is called, which allows a class to implement its own new/delete operators. This is often done when you have frequently allocated classes of small objects and want to keep a cache of allocated space that can be used as necessary, the object (sic) of which is to reduce memory fragmentation at run time. In such a case, the function new would find an unused buffer and call the placement new operator on it. Placement new allows you to construct an object using an existing buffer. Application code can do this (placement new) as well. It is often useful, especially in memory-constrained systems or systems where you don't want to have much dynamic memory, such as embedded systems.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  10. #9
    Just Joined!
    Join Date
    May 2009
    Posts
    21
    Finally I think I got answer, Please let me know if i am wrong:
    -Who will create memory for a class object(compiler or constructor or anyother)
    -Who will call the constructor when an oject is created.

    If we are creating an static object, then compiler will takecare of allocating and calling constructor. But, if we are creating an object using 'new' then 'operator new' will take care.
    Like operator new has two responsibilities:
    1. calling 'new operator' to create memory area.
    2. once it is done, it calls the constructor associated to it.

    your answers has given me clue to debug it.

    Thanks for your replies,
    Madhu

  11. #10
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,753
    You might want to invest in "The Annotated C++ Reference Manual - ANSI Base Document", popularly known as the ARM, by Ellis and Stroustrup for all of these sort of gory details. Stroustrup is the inventor of the C++ language.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •