diff --git a/CPP_Learn/CPP_Learn.cpp b/CPP_Learn/CPP_Learn.cpp index 972d1cd..e92cc86 100644 --- a/CPP_Learn/CPP_Learn.cpp +++ b/CPP_Learn/CPP_Learn.cpp @@ -1,6 +1,6 @@ #include -int main() -{ - std::cout << "Hello World!\n"; -} \ No newline at end of file +//int main() +//{ +// std::cout << "Hello World!\n"; +//} \ No newline at end of file diff --git a/CPP_Learn/CPP_Learn.vcxproj b/CPP_Learn/CPP_Learn.vcxproj index 0a1010e..4a7d454 100644 --- a/CPP_Learn/CPP_Learn.vcxproj +++ b/CPP_Learn/CPP_Learn.vcxproj @@ -128,6 +128,7 @@ + diff --git a/CPP_Learn/CPP_Learn.vcxproj.filters b/CPP_Learn/CPP_Learn.vcxproj.filters index 4ac21f0..0edc224 100644 --- a/CPP_Learn/CPP_Learn.vcxproj.filters +++ b/CPP_Learn/CPP_Learn.vcxproj.filters @@ -18,5 +18,8 @@ 源文件 + + 源文件 + \ No newline at end of file diff --git a/CPP_Learn/MemoryAlignment.cpp b/CPP_Learn/MemoryAlignment.cpp new file mode 100644 index 0000000..9163892 --- /dev/null +++ b/CPP_Learn/MemoryAlignment.cpp @@ -0,0 +1,76 @@ +#include +#include +using namespace std; + +typedef struct { + int a; + int* p; + char c; + short s; + double d; + bool b; + short sArr[4]; +}struct1; + +typedef struct { + short sh; + struct1 s1; + char c; +}struct2; + +typedef struct { + char c; + vector vec; + short s; +}struct3; + +void test() { + int i = 10; + struct1 s1 = { + 1234, + &i, + 'c', + 123, + 123.4, + true, + {1, 2, 3, 4} + }; + + cout << "size of struct1 : " << sizeof(s1) << endl; + cout << "offset of a : " << offsetof(struct1, a) << endl; + cout << "offset of p : " << offsetof(struct1, p) << endl; + cout << "offset of c : " << offsetof(struct1, c) << endl; + cout << "offset of s : " << offsetof(struct1, s) << endl; + cout << "offset of d : " << offsetof(struct1, d) << endl; + cout << "offset of b : " << offsetof(struct1, b) << endl; + cout << "offset of sArr : " << offsetof(struct1, sArr) << endl; + cout << "--------------" << endl << endl; + + struct2 s2; + s2.sh = 42; + s2.c = 'f'; + + cout << "size of struct2 : " << sizeof(s2) << endl; + cout << "offset of s1 : " << offsetof(struct2, s1) << endl; + cout << "offset of c : " << offsetof(struct2, c) << endl; + cout << "--------------" << endl << endl; + + struct3 s3; + s3.c = 'A'; + s3.s = 678; + cout << "size of struct3 : " << sizeof(s3) << endl; + cout << "offset of c : " << offsetof(struct3, c) << endl; + cout << "offset of vec : " << offsetof(struct3, vec) << endl; + cout << "offset of s : " << offsetof(struct3, s) << endl; + cout << "--------------" << endl << endl; + + for (int j = 0; j < 100; j++) { + s3.vec.push_back(j + 1); + } + + cout << "done" << endl; +} + +int main() { + test(); +} \ No newline at end of file diff --git a/CPP_Learn/x64/Debug/CPP_Learn.ilk b/CPP_Learn/x64/Debug/CPP_Learn.ilk index 6e8f09f..85123d3 100644 Binary files a/CPP_Learn/x64/Debug/CPP_Learn.ilk and b/CPP_Learn/x64/Debug/CPP_Learn.ilk differ diff --git a/CPP_Learn/x64/Debug/CPP_Learn.log b/CPP_Learn/x64/Debug/CPP_Learn.log index 1db99e4..c2bd97b 100644 --- a/CPP_Learn/x64/Debug/CPP_Learn.log +++ b/CPP_Learn/x64/Debug/CPP_Learn.log @@ -1,2 +1,2 @@ - CPP_Learn.cpp + MemoryAlignment.cpp CPP_Learn.vcxproj -> E:\22020100011_LXB\Projects\VisualStudio\CPP_Learn\x64\Debug\CPP_Learn.exe diff --git a/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.command.1.tlog b/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.command.1.tlog index d3c17ae..976e537 100644 Binary files a/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.command.1.tlog and b/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.command.1.tlog differ diff --git a/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.read.1.tlog b/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.read.1.tlog index d9fc173..c771408 100644 Binary files a/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.read.1.tlog and b/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.read.1.tlog differ diff --git a/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.write.1.tlog b/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.write.1.tlog index a1d418f..2c091ee 100644 Binary files a/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.write.1.tlog and b/CPP_Learn/x64/Debug/CPP_Learn.tlog/CL.write.1.tlog differ diff --git a/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.command.1.tlog b/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.command.1.tlog index 58e0d08..cad6ac0 100644 Binary files a/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.command.1.tlog and b/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.command.1.tlog differ diff --git a/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.read.1.tlog b/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.read.1.tlog index b123ca6..2f93f83 100644 Binary files a/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.read.1.tlog and b/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.read.1.tlog differ diff --git a/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.write.1.tlog b/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.write.1.tlog index e1ca210..e33ecfe 100644 Binary files a/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.write.1.tlog and b/CPP_Learn/x64/Debug/CPP_Learn.tlog/link.write.1.tlog differ diff --git a/CPP_Learn/x64/Debug/vc143.idb b/CPP_Learn/x64/Debug/vc143.idb index cf27fb0..9c4880b 100644 Binary files a/CPP_Learn/x64/Debug/vc143.idb and b/CPP_Learn/x64/Debug/vc143.idb differ diff --git a/CPP_Learn/x64/Debug/vc143.pdb b/CPP_Learn/x64/Debug/vc143.pdb index 3e388eb..a471e80 100644 Binary files a/CPP_Learn/x64/Debug/vc143.pdb and b/CPP_Learn/x64/Debug/vc143.pdb differ