C++ Tutorial/Operators statements/sizeof
Содержание
Demonstrate sizeof.
<source lang="cpp">#include <iostream> using namespace std;
int main() {
char ch; int i; cout << sizeof ch << " "; // size of char cout << sizeof i << " "; // size of int cout << sizeof (float) << " "; // size of float cout << sizeof (double) << " "; // size of double return 0;
}</source>
1 4 4 8
Object sizes
<source lang="cpp">#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;
}</source>
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.
sizeof a class
<source lang="cpp">/* The following code example is taken from the book
* "C++ Templates - The Complete Guide" * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002 * * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
class EmptyClass { }; int main() {
std::cout << "sizeof(EmptyClass): " << sizeof(EmptyClass) << "\n";
}</source>
sizeof(EmptyClass): 1
sizeof a union
<source lang="cpp">#include<iostream.h> union u_tag{
int i; double d;
}u={88}; struct s_tag{
int i; double d;
}s={66,1.234}; int main() {
int size; size=sizeof(union u_tag); cout<<"sizeof(union u_tag)="<<size<<endl; u.i=100; cout<<"u.i="<<u.i<<endl; u.d=1.2345; cout<<"u.d="<<u.d<<endl; size=sizeof(u.d); cout<<"sizeof(u.d)="<<size<<endl; cout<<"s.i="<<s.i<<endl; cout<<"s.d="<<s.d<<endl; size=sizeof(struct s_tag); cout<<"sizeof(struct s_tag)="<<size<<endl;
}</source>
sizeof(union u_tag)=8 u.i=100 u.d=1.2345 sizeof(u.d)=8 s.i=66 s.d=1.234 sizeof(struct s_tag)=16
Use sizeof operator on an array name: returns the number of bytes in the array
<source lang="cpp">#include <iostream> using std::cout; using std::endl; int main() {
double array[ 20 ]; cout << "The number of bytes in the array is " << sizeof( array ); return 0;
}</source>
The number of bytes in the array is 160
Using the sizeof operator for base class and derived class
<source lang="cpp">#include <iostream>
- include <ostream>
class base {}; class derived : public base {}; int main() {
using namespace std; cout << sizeof(base) << "\n"; cout << sizeof(derived) << "\n"; base b[3]; cout << sizeof(b) << "\n"; derived d[5]; cout << sizeof(d) << "\n";
}</source>
1 1 3 5