Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I would like to know the details of memory usage under the following scenarios.
1. Suppose 10 objects are created of the same class. Will all these objects share the same memory?
2. In C, we declare an integer as "int i;". In C++ , when we declare "i" as member variable, both "int i" and member variable "i" take the same size?

Is there any tutorial/ link for understanding these basics?
Posted
Updated 8-May-11 21:16pm
v2

1. No. They are separate instances in memory.
2. Yes. Even though padding and alignment might be different.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-11 3:39am    
Correct, a 5. (venkatmakam provided more details).
--SA
1.Suppose 10 objects are created of the same class. Will all these objects share the same memory?
No the class objects wont share the memory.Each object will have its own memory allocation.But, Static member of a class will be shared by all its objects.

2.In C, we declare an integer as "int i;". In C++ , when we declare "i" as member variable, both "int i" and member variable "i" take the same size?
Yes Both will have same memory size.

Links:
1. http://www.cplusplus.com/doc/tutorial/[^]

2. Use a ANSI compiler and learn debugging.This will help you to clarify this types of doubts
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-11 3:39am    
More detailed. My 5.
--SA
Joan M 9-May-11 3:40am    
Nice answer... 5!
ShilpiP 9-May-11 3:49am    
Nice +5... :)
reshmi2000 9-May-11 4:13am    
But as Shilpi Boosar told, what i understand is -the member functions will be shared by all objects, with sharing done using "this" pointer? And, member variables are stored in seperate memory, except for "static" . Am i right?
Yes Shilpi is right.The functions(code memory) will be shared by all objects."this" pointer is used to refer the class member variables in the shared functions.refer the following link how "this" pointer is used in functions.

http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr035.htm
1) Class is a template or blueprint that have functions and member variable. Object is an instance of class. When we create an object than a memory is allocated to that object. This memory is equivalent to the memory of member variable.Exception is if member variable is static than it is shared among all object.functions are loaded once in a memory and shared by every object. sharing of function is done with the help of "this" pointer.
2) Yes. The size of int is always same. The size of int depends on compiler and processor.

Book : Herbert Schildt c++ complete reference
->[^]
 
Share this answer
 
Comments
More detailed explanation on this pointer.My 5 too.
ShilpiP 10-May-11 0:58am    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900