Mega Code Archive

 
Categories / C++ Tutorial / Operators Statements
 

Object sizes

#include <iostream> using std::cout; using std::endl; class Box {   public:     Box() :length(1.0), width(1.0), height(1.0), pMaterial("new") {}          int totalSize() {       return sizeof(length) + sizeof(width) + sizeof(height) + sizeof(pMaterial );     }   private:     char* pMaterial;     double length;     double width;     double height; }; int main() {   Box box;   Box boxes[10];   cout << endl            << "The data members of a Box object occupy "        << box.totalSize() << " bytes.";   cout << endl           << "A single Box object occupies "        << sizeof (Box) << " bytes.";   cout << endl           << "An array of 10 Box objects occupies "        << sizeof(boxes)  << " bytes."        << endl;   return 0; } The data members of a Box object occupy 28 bytes. A single Box object occupies 32 bytes. An array of 10 Box objects occupies 320 bytes.