windows查看内存布局

📅 发布时间:2026/7/9 4:41:18
windows查看内存布局 cl ConsoleApplication3.cpp /d1 reportSingleClassLayout[classname] /d1 reportAllClassLayout 可以查看源文件中所有类及结构体的内存布局 classname为类名/d1 reportSingleClassLayout[classname]中间没有空格 加上#includeiostream 会有很多额外的输出在测试时可以注释掉struct S { char x; int y; double z; }; class TestClass { private: char y; double z; int x; }; class Base { private: int x; public: virtual ~Base(); virtual void f1(); virtual int g1(); }; class Derived : public Base { private: char y; public: virtual float f2(); }; class Derived2 : public Base { private: double z; public: virtual void f1(); virtual float v2(); int f3(); }; class Base2 { private: int yy; public: virtual void g2(); }; class Derived3 : public Base, public Base2 { private: double zz; public: virtual void g3(); }; // cl ConsoleApplication3.cpp /d1 reportSingleClassLayoutS // cl ConsoleApplication3.cpp /d1 reportSingleClassLayoutTestClass // cl ConsoleApplication3.cpp /d1 reportSingleClassLayoutBase // cl ConsoleApplication3.cpp /d1 reportSingleClassLayoutDerived // cl ConsoleApplication3.cpp /d1 reportSingleClassLayoutDerived2 // cl ConsoleApplication3.cpp /d1 reportSingleClassLayoutDerived3class Test { public: Test(); ~Test(); virtual void fun(); }; struct st1 { short number; float grade; float grade2; float grade3; char level; }; struct st2 { char level; short number; float grade; float grade2; float grade3; }; #pragma pack(1) struct st3 { char level; short number; float grade; float grade2; float grade3; }; #pragma pack() void TestSizeOf() { cout __FUNCTION__ endl; cout sizeof(short) sizeof(short) endl; cout sizeof(st1) sizeof(st1) endl; cout offsetof(st1, number) offsetof(st1, number) endl; cout offsetof(st1, grade) offsetof(st1, grade) endl; cout offsetof(st1, grade2) offsetof(st1, grade2) endl; cout offsetof(st1, grade3) offsetof(st1, grade3) endl; cout offsetof(st1, level) offsetof(st1, level) endl; cout sizeof(st2) sizeof(st2) endl; cout offsetof(st2, level) offsetof(st2, level) endl; cout offsetof(st2, number) offsetof(st2, number) endl; cout offsetof(st2, grade) offsetof(st2, grade) endl; cout offsetof(st2, grade2) offsetof(st2, grade2) endl; cout offsetof(st2, grade3) offsetof(st2, grade3) endl; cout sizeof(st3) sizeof(st3) endl; cout offsetof(st3, level) offsetof(st3, level) endl; cout offsetof(st3, number) offsetof(st3, number) endl; cout offsetof(st3, grade) offsetof(st3, grade) endl; cout offsetof(st3, grade2) offsetof(st3, grade2) endl; cout offsetof(st3, grade3) offsetof(st3, grade3) endl; }