用union节省内存

#include<iostream>
using namespace std;
union packed
{
    char i;
    short j;
    int k;
    long l;
    float f;
    double d;
};
int main()
{
    cout << "sizeof(packed) = " << sizeof(packed) << endl;
    packed x;
    x.i = 'c';
    cout << x.i << endl;
    x.d = 3.1415926;
    cout << x.d << endl;
    cout << x.i << endl;
    return 0;
}


/*


sizeof(packed) = 8
c
3.14159
J


*/

你可能感兴趣的:(用union节省内存)