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.


Share the Post: