C++ memory allocation for objects

C++ Memory allocation for objects

we have stated that the memory space for object is allocated when they are declared and not when the class is specified . This statement is only partly true. Basically the member functions are created and placed in the memory space only once when they are defined as a part of a class specification. since all the objects belonging to that class uses the same member functions, whenever the objects are created no separate space is allocated for member functions . only space for member variables is allocated separately for each object. separate memory locations for the objects are essential because the member variable will hold different data values for different object.

This is show below fig

memory allocation for objects in c++

STATIC DATA MEMBERS

A data members of a class can be qualified as static. The properties of a static member variable are similar to that of a C language static variable. A static member variable has certain special characteristics These are

1. it is initialize to 0(zero) when first object of its class is created. No other initialization is permitted.
2. Only single copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
3. it’s visible only within the class, but its lifetime is the entire program

Static variable are normally used to maintain the common values to the entire class. lets see for example a static data member can be used as a counter that records the occurrences of all the objects. in the below program show static data member

#include<iostream>
using namespace std;
class items
{
static int count;
int number;
public:
void getdata(int a)
{
number=a;
count++;
}
void getcount()
{
std::cout<<"count is"<<count <<"\n";
}
};
int items:: count;
int main()
{
items x,y,z; // count is initialized to zero
x.getcount(); // display count
y.getcount();
z.getcount();
x.getdata(100); // getting data into object x
y.getdata(200); // getting data into object y
z.getdata(300); // getting data into object z
cout << "After reading data" << "\n";
x.getcount(); // display count
y.getcount();
z.getcount();
return 0;
}

The output will be
count is 0
count is 0
count is 0
After reading data
count is 3
count is 3
count is 3

Notice The following statement in the program

int items:: count; // definition of static data member

Note that The type and scope of each static member variable must be defined outside the class definition. This is necessary because the static data members are stored separately rather than as a part of an object. Since they are associated with the class itself rather than with any class object, they are also known as class variables.

The static variable count is initialized to zero when the object are created. The count is incremented whenever the data is read into an object. since the data is read into objects three times, the variable count is incremented three times. because there is only one copy of count shared by all the three objects, all the three output statements cause the value 3 to be displayed

in the below fig show

Static data members

static variables are like non line member functions as they are declared in a class declaration and defined in the source file. while defining a static variable, some initial value can also be assigned to the variable. for instance the following definition gives count the initial value 10

int item::count=10;

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise