系统相关
首页 > 系统相关> > C++ 类对象的内存大小分析

C++ 类对象的内存大小分析

作者:互联网

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

class Base
{
public:

};
int _tmain(int argc, _TCHAR* argv[])
{
	Base b;
	int size = sizeof(b);
	cout << size << endl;
	return 0;
}

在这里插入图片描述

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

class Base
{
public:
	void fun1(){};
	void fun2(){};
};
int _tmain(int argc, _TCHAR* argv[])
{
	Base b;
	int size = sizeof(b);
	cout << size << endl;
	return 0;
}


在这里插入图片描述

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

class Base
{
public:
	//void fun1(){};
	//void fun2(){};
	int a;
};
int _tmain(int argc, _TCHAR* argv[])
{
	Base b;
	int size = sizeof(b);
	cout << size << endl;
	return 0;
}


在这里插入图片描述

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

class Base
{
public:
	//void fun1(){};
	//void fun2(){};
	//int a;
	virtual void fun3(){};
	//static int b;
	//static void fun4(){};
};
int _tmain(int argc, _TCHAR* argv[])
{
	Base b;
	int size = sizeof(b);
	cout << size << endl;
	return 0;
}

在这里插入图片描述

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

class Base
{
public:
	//void fun1(){};
	//void fun2(){};
	//int a;
	static int b;
	static void fun4(){};
};
int _tmain(int argc, _TCHAR* argv[])
{
	Base b;
	int size = sizeof(b);
	cout << size << endl;
	return 0;
}

在这里插入图片描述

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

class Base
{
public:
	void fun1(){};
	void fun2(){};
	int a; // 占4字节
	virtual void fun3(){}; //占4字节
	static int b;
	static void fun4(){};

	char c; //占4字节 (内存对齐)
};
int _tmain(int argc, _TCHAR* argv[])
{
	Base b;
	int size = sizeof(b);
	cout << size << endl;
	return 0;
}

在这里插入图片描述
总结:

标签:stdafx,cout,对象,void,内存大小,C++,int,Base,include
来源: https://blog.csdn.net/weixin_41552975/article/details/121036086