Object Oriented Programming: Chapter 2: Classes and Objects

Memory Allocation for Objects

C++ Programming | Object Oriented Programming

Questions: 1. Discuss instantiation of objects. 2. Discuss memory allocation for objects.

Memory Allocation for Objects

• The memory space objects is allocated when they are declared.

The members functions are created and placed in the memory only when they are defined as a part of a class specification.

• As the objects are using the same old member functions of the belonging class, no separate memory space is created for these  function when objects are created.

• Only for the member variables the separate memory space is created for each object.

• Fig. 2.12.1 illustrates this concept.


•  The memory required by any object is dependent upon following factors

 1. size of non static data members of the class.

2. order of data members.

3. Byte padding.


Consider following example ‒

class Test

{

    float a;

    int b;

    static int c;

     char d;

};

Test obj;

For obj size will be sizeof(a)+sizeof(b)+sizeof(d)=4+2+1=7 bytes. Note that the size of static members is not included in this calculation.Static members are really not part of the class object. They won't be included in object's layout.

 

Review Questions

1. Discuss instantiation of objects.

2. Discuss memory allocation for objects.

 

Object Oriented Programming: Chapter 2: Classes and Objects : Tag: Oops, Computer Programming : C++ Programming | Object Oriented Programming - Memory Allocation for Objects


Object Oriented Programming: Chapter 2: Classes and Objects



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation