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 ...
- 08-06-2010 #1Just 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
- 08-06-2010 #2Linux Newbie
- Join Date
- Mar 2010
- Posts
- 121
I guess that depends on whether it's created on the stack or on the heap. If you declare an object like this:
then the compiler will set aside the necessary stack space. If you declare it like this:Code:Object obj;
Then the C++ standard library will allocate the memory on the heap for you.Code:Object *obj = new Object;
The compiler will ensure the constructors are always called - the constructor is automatically called for you in both the above examples.
- 08-06-2010 #3Just 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.
- 08-06-2010 #4Linux Newbie
- Join Date
- Mar 2010
- Posts
- 121
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.
- 08-09-2010 #5Just 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.
- 08-17-2010 #6Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
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!
- 08-18-2010 #7Just 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.
- 08-18-2010 #8Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
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!
- 08-19-2010 #9Just 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
- 08-19-2010 #10Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
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!


Reply With Quote
