Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Memory_test
#ifndef __FRUIT__ #define __FRUIT__ //原始定义 class Fruit { int no; double weight; char key; public: void print() { } virtual void process() { } }; class Apple : public Fruit { int size; char type; public: void save() { } virtual void process() { } }; //去掉虚函数后的定义 class Fruit1 { int no; double weight; char key; public: void print() { } //virtual void process() { } }; class Apple1 : public Fruit1 { int size; char type; public: void save() { } //virtual void process() { } }; //添加构造函数后的定义 class Fruit2 { int no; double weight; char key; public: Fruit2(int n, double w, char k) :no(n), weight(w), key(k) {} void print() { } virtual void process() { } }; class Apple2 : public Fruit2 { int size; char type; public: Apple2(int s, char t, int n, double w, char k) :size(s), type(t), Fruit2(n, w, k) {} void save() { } virtual void process() { } }; //父类子类虚函数不同名的情况 class Fruit3 { int no; double weight; char key; public: Fruit3(int n, double w, char k) :no(n), weight(w), key(k) {} void print() { } virtual void process1() { } }; class Apple3 : public Fruit3 { int size; char type; public: Apple3(int s, char t, int n, double w, char k) :size(s), type(t), Fruit3(n, w, k) {} void save() { } virtual void process2() { } }; //定义顺序调整(虚函数同名)(优化后) class Fruit4 { char key; int no; double weight; public: Fruit4(int n, double w, char k) :no(n), weight(w), key(k) {} void print() { } virtual void process() { } }; class Apple4 : public Fruit4 { char type; int size; public: Apple4(int s, char t, int n, double w, char k) :Fruit4(n, w, k), size(s), type(t){} void save() { } virtual void process() { } }; //纯虚函数是否影响 class Fruit5 { int no; double weight; char key; public: Fruit5(int n, double w, char k) :no(n), weight(w), key(k) {} void print() { } virtual void process() = 0; }; class Apple5 : public Fruit5 { int size; char type; public: Apple5(int s, char t, int n, double w, char k) :size(s), type(t), Fruit5(n, w, k) {} void save() { } void process() { } }; /**/ //虚函数的大小为多少 class Fruit6 { public: Fruit6() {} void print() { } virtual void process() {}; }; class Apple6 : public Fruit6 { public: Apple6() {} void save() { } void process() { } }; //验证虚函数和对齐 class Fruit7 { char n; public: Fruit7(char a):n(a) {} void print() { } virtual void process() {}; }; class Apple7 : public Fruit7 { char x; public: Apple7(char a, char b):Fruit7(a), x(b) {} void save() { } void process() { } }; //测试顺序的影响 class Fruit8 { char key; int no; double weight; public: void print() { } virtual void process() { } }; class Apple8 : public Fruit8 { char type; int size; public: void save() { } virtual void process() { } }; //测试顺序的影响 class Fruit9 { char key; char x; int no; double weight; public: void print() { } Fruit9(char a, char b, int n, double w) :key(a), x(b), no(n), weight(w){} virtual void process() { } }; class Apple9 : public Fruit9 { char type; int size; public: Apple9(char t, int s, char a, char b, int n, double w) :Fruit9(a, b, n , w), type(t), size(s) {} void save() { } virtual void process() { } };/**/ //多个虚函数 class Fruit10 { int no; double weight; char key; public: void print() { } Fruit10(char a, int n, double w) :key(a), no(n), weight(w) {} virtual void process1() { } virtual void process2() { } virtual void process3() { } }; class Apple10 : public Fruit10 { int size; char type; public: Apple10(char t, int s, char a, int n, double w) :Fruit10(a, n, w), type(t), size(s) {} void save() { } virtual void process1() { } virtual void process2() { } virtual void process4() { } };/**/ #endif // __FRUIT__ #include <iostream> #include <string> #include <iomanip> using namespace std; string operator*(string z, int n) { string temp = z; for (int i = 0; i < n; i++) { z += temp; } return z; } void printMemo(char* name, void* f, int size) { string s = "-"; cout << "the memory of " << name << s*20 <<"\n"; cout << "Address of "<<name << ": 0x " << hex << f << " | Size = " << dec <<size <<endl; if (!f) { cout << "Null pointer exception\n"; cout << s * 30 << "\n\n"; return; } unsigned char* x = (unsigned char*)f; for (int i = 0; i < size; i++) { cout << setfill('0') << setw(2) << hex << (unsigned int)*x << " "; if (!((i + 1) % (size>8? 8: 4))) { cout << "\n"; } x++; } cout << s*30 <<"\n\n"; } int main() { string s = "-"; cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of double : " << sizeof(double) << "\n" << s * 30 << endl; cout << "size of Fruit is " << sizeof(Fruit) << endl; cout << "size of Apple is " << sizeof(Apple) << "\n" << s * 30 << "\n\n"<<endl; cout << "测试输出最原始结构" << endl; Fruit f; Fruit* ft = &f; printMemo("Fruit", ft, sizeof(Fruit)); Apple a; Apple* at = &a; printMemo("Apple", at, sizeof(Apple)); cout << "去掉虚函数后的定义" << endl; Fruit1 f1; Fruit1* ft1 = &f1; printMemo("Fruit1", ft1, sizeof(Fruit1)); Apple1 a1; Apple1* at1 = &a1; printMemo("Apple1", at1, sizeof(Apple1)); cout << "添加构造函数后的定义" << endl; Fruit2 f2(1, 2.345, 'c'); Fruit2* ft2 = &f2; printMemo("Fruit2", ft2, sizeof(Fruit2)); Apple2 a2(9, 'd', 1, 2.345, 'c'); Apple2* at2 = &a2; printMemo("Apple2", at2, sizeof(Apple2)); cout << "父类子类虚函数不同名的情况" << endl; Fruit3 f3(1, 3.456, 'c'); Fruit3* ft3 = &f3; printMemo("Fruit3", ft3, sizeof(Fruit3)); Apple3 a3(9, 'd', 1, 3.456, 'c'); Apple3* at3 = &a3; printMemo("Apple3", at3, sizeof(Apple3)); cout << "定义顺序调整(虚函数同名)(优化后)" << endl; Fruit4 f4(1, 4.456, 'c'); Fruit4* ft4 = &f4; printMemo("Fruit4", ft4, sizeof(Fruit4)); Apple4 a4(9, 'd', 1, 4.456, 'c'); Apple4* at4 = &a4; printMemo("Apple4", at4, sizeof(Apple4)); cout << "纯虚函数是否影响" << endl; //抽象类不能创建对象 printMemo("Fruit5", NULL, sizeof(Fruit5)); Apple5 a5(9, 'd', 1, 5.556, 'c'); Apple5* at5 = &a5; printMemo("Apple5", at5, sizeof(Apple5)); cout << "测试元素分布" << endl; //验证位置(未优化1) Fruit2 f21(1, 2.345, 'c'); Fruit2* ft21 = &f21; printMemo("Fruit21", ft21, sizeof(Fruit2)); //验证位置(未优化2) Fruit2 f22(3, 2.345, 'b'); Fruit2* ft22 = &f22; printMemo("Fruit22", ft22, sizeof(Fruit2)); //验证位置(未优化1) Apple2 a21(9, 'd', 2, 6.789, 'e'); Apple2* at21 = &a21; printMemo("Apple21", at21, sizeof(Apple2)); //验证位置(未优化2) Apple2 a22(8, 'a', 3, 6.789, 'f'); Apple2* at22 = &a22; printMemo("Apple21", at22, sizeof(Apple2)); //验证位置(优化后1) Fruit4 f41(1, 41.4156, 'c'); Fruit4* ft41 = &f41; printMemo("Fruit41", ft41, sizeof(Fruit4)); //验证位置(优化后2) Fruit4 f42(2, 41.4156, 'b'); Fruit4* ft42 = &f42; printMemo("Fruit42", ft42, sizeof(Fruit4)); Apple4 a41(9, 'd', 1, 41.4156, 'c'); Apple4* at41 = &a41; printMemo("Apple41", at41, sizeof(Apple4)); Apple4 a42(8, 'e', 1, 41.4156, 'g'); Apple4* at42 = &a42; printMemo("Apple42", at42, sizeof(Apple4)); /**/ cout << "测试虚函数的大小" << endl; Fruit6 f6; Fruit6* ft6 = &f6; printMemo("Fruit6", ft6, sizeof(Fruit6)); Apple6 a6; Apple6* at6 = &a6; printMemo("Apple6", at6, sizeof(Apple6)); cout << "验证虚函数和对齐" << endl; Fruit7 f7('a'); Fruit7* ft7 = &f7; printMemo("Fruit7", ft7, sizeof(Fruit7)); Apple7 a7('b', 'c'); Apple7* at7 = &a7; printMemo("Apple7", at7, sizeof(Apple7)); cout << "测试定义顺序对内存的影响" << endl; Fruit8 f8; Fruit8* ft8 = &f8; printMemo("Fruit8", ft8, sizeof(Fruit8)); Apple8 a8; Apple8* at8 = &a8; printMemo("Apple8", at8, sizeof(Apple8)); Fruit9 f9('a', 'b', 1, 1.234); Fruit9* ft9 = &f9; printMemo("Fruit9", ft9, sizeof(Fruit9)); Apple9 a9('c', 2, 'a', 'b', 1, 1.234); Apple9* at9 = &a9; printMemo("Apple9", at9, sizeof(Apple9)); /**/ cout << "多个虚函数测试,对齐情况" << endl; Fruit10 f10(1, 10.1056, 'c'); Fruit10* ft10 = &f10; printMemo("Fruit10", ft10, sizeof(Fruit10)); Apple10 a10(9, 'd', 1, 10.1056, 'c'); Apple10* at10 = &a10; printMemo("Apple10", at10, sizeof(Apple10)); return 0; }
run
|
edit
|
history
|
help
0
offsetof
3 and 7 in a row
parallel_for_each
Backpack with recursion
Primality Test | Fermat
Dar
cref
iuadhfaoiufs
Test02
kroliki