C++ 查看類的內存大小

#include <iostream>

using namespace std;

class Demo
{
private:
    int x;
    double y;
};



int main()
{
	cout << sizeof(Demo) << endl;
	cout << sizeof(char) << endl;
	cout << sizeof(int) << endl;
	cout << sizeof(double) << endl;
	
	cout << sizeof(Demo*) << endl;
	cout << sizeof(char*) << endl;
	cout << sizeof(int*) << endl;
	cout << sizeof(double*) << endl;
	return 0;
}


使用sizeof() 函數。

如果類Demo中的double爲指針的話,則類Demo的大小爲8 。爲double類型的話,則類Demo的大小爲16,不知道原因。

發佈了94 篇原創文章 · 獲贊 19 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章