计算class,struct,uion的sizeof(),struct与uion的区别
2013-08-29 11:08
429 查看
#include "stdafx.h"
#include "iostream"
#include "conio.h"
using namespace std;
//类中只有成员变量与虚函数参与计算
class A
{
public:
virtual void funa() ;//4个字节
virtual void funb() ;//4个字节
void fun() ;
static void fund() ;
static int si ;
A() ;
~A() ;
long a ;//4个字节,补齐8个字节
double d;//8个字节
private:
int i ;//4个字节
char c ;//补齐4个字节
};
//结构中字节不足要补齐
struct B
{
short a;//2个字节,补齐有4个字节
long b;//4个字节
double d ;//8个字节
long double dd ;//8个字节
char c;//1个字节,补齐8个字节
}bb;
//union中字节取最大字节并是最小字节的最小倍数,且赋值过的变量的值是由最后一次赋值决定
union UI
{
short a; //2个字节;
long b ;//4个字节
double d ;//8个字节
char c[9] ;//9个字节
};
union C
{
short a; //2个字节;
long b ;//4个字节
double d ;//8个字节
char c[5] ;//5个字节
};
/*
或
union C
{
short a; //2个字节;
long b ;//4个字节
//double d ;//8个字节
char c[5] ;//5个字节
};
*/
int _tmain(int argc, _TCHAR* argv[])
{
int num[5] = {1,2,3,4,5} ;
cout<<"sizeof(A)="<<sizeof(A)<<endl ;//输出32
cout<<"sizeof(B)="<<sizeof(B)<<endl ;//输出32
cout<<"sizeof(UI)="<<sizeof(UI)<<endl ;//输出16
cout<<"sizeof(C)="<<sizeof(C)<<endl ;//输出8
UI ui ;
ui.a = 11 ;
ui.b = 1 ;
cout<<"ui.a="<<ui.a<<",ui.b="<<ui.b <<endl;// 输出1 1
getch() ;
return 0;
}
#include "iostream"
#include "conio.h"
using namespace std;
//类中只有成员变量与虚函数参与计算
class A
{
public:
virtual void funa() ;//4个字节
virtual void funb() ;//4个字节
void fun() ;
static void fund() ;
static int si ;
A() ;
~A() ;
long a ;//4个字节,补齐8个字节
double d;//8个字节
private:
int i ;//4个字节
char c ;//补齐4个字节
};
//结构中字节不足要补齐
struct B
{
short a;//2个字节,补齐有4个字节
long b;//4个字节
double d ;//8个字节
long double dd ;//8个字节
char c;//1个字节,补齐8个字节
}bb;
//union中字节取最大字节并是最小字节的最小倍数,且赋值过的变量的值是由最后一次赋值决定
union UI
{
short a; //2个字节;
long b ;//4个字节
double d ;//8个字节
char c[9] ;//9个字节
};
union C
{
short a; //2个字节;
long b ;//4个字节
double d ;//8个字节
char c[5] ;//5个字节
};
/*
或
union C
{
short a; //2个字节;
long b ;//4个字节
//double d ;//8个字节
char c[5] ;//5个字节
};
*/
int _tmain(int argc, _TCHAR* argv[])
{
int num[5] = {1,2,3,4,5} ;
cout<<"sizeof(A)="<<sizeof(A)<<endl ;//输出32
cout<<"sizeof(B)="<<sizeof(B)<<endl ;//输出32
cout<<"sizeof(UI)="<<sizeof(UI)<<endl ;//输出16
cout<<"sizeof(C)="<<sizeof(C)<<endl ;//输出8
UI ui ;
ui.a = 11 ;
ui.b = 1 ;
cout<<"ui.a="<<ui.a<<",ui.b="<<ui.b <<endl;// 输出1 1
getch() ;
return 0;
}
相关文章推荐
- C语言中union与struct的区别及sizeof的计算方式
- C# struct class 在Marshal.SizeOf 的区别
- 面试珠玑 C语言中union与struct的区别及sizeof的计算方式
- c/c++中使用sizeof计算结构体struct和类class的长度(字节)
- 标题:C++中struct与class的区别是什么?
- C++ class和struct的区别
- C# 中 Struct 与 Class 的区别,以及两者的适用场合
- 关于Union,Struct and Class的大小计算问题
- c++中class与struct的区别
- strlen()与sizeof()区别、计算及字节对齐、位域
- Struct和Class的区别
- struct和class的区别
- C#中Struct与Class的区别
- C++中关键字Struct和Class的区别
- c++中struct和class的区别
- C#中Struct与Class的区别
- C++中的struct和class的区别
- C++中类class与结构体struct的异同区别
- C语言中,当计算字符数组长度时,用sizeof 和strlen 的原理及两者的区别
- C++中Struct和Class的区别