Memory Layout of Objects

Memory Layout of Objects

If a program constructs an object (let’s say, foo) that has a number of large and involved methods 100 times, does it duplicate in memory all the code for foo’s methods 100 times?

Of course not. The C++ object model separates between data members and member functions. Data members are duplicated (i.e., each object instance gets a private set of these members—except for static data members, which are shared by all instances). The member functions code, on the other hand, is shared. Each object in the array executes the same constructor code in its turn.

See also  How IoT Tech Will Transform Logistics

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist