Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Preserving strict aliasing Union example
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 #include <iostream> #include <windows.h> int main() { // Example of using Union to create binary compability type // It preserves strict aliasing rule union FILE_NAME_INFO_UNION { FILE_NAME_INFO info; struct MY_FILENAME_INFO { DWORD FileNameLength; WCHAR FileName[MAX_PATH]; } myInfo; }; FILE_NAME_INFO_UNION u = FILE_NAME_INFO_UNION(); std::wcout << L"sizeof: " << sizeof(u) << std::endl; std::wcout << L"FileNameLength offset 1: " << offsetof(FILE_NAME_INFO_UNION,info.FileNameLength) << std::endl; std::wcout << L"FileNameLength offset 2: " << offsetof(FILE_NAME_INFO_UNION,myInfo.FileNameLength) << std::endl; std::wcout << L"FileName offset 1: " << offsetof(FILE_NAME_INFO_UNION,info.FileName) << std::endl; std::wcout << L"FileName offset 2: " << offsetof(FILE_NAME_INFO_UNION,myInfo.FileName) << std::endl; std::wstring hello = L"Hello World!"; // Copy to system struct: u.info.FileNameLength = hello.size(); wcscpy(u.info.FileName,hello.c_str()); // Read from myInfo struct: std::wcout << L"Output: " << u.myInfo.FileNameLength << L" " << u.info.FileName << std::endl; }
run
|
edit
|
history
|
help
0
Removing __unaligned specifier partial solution
"Mostly invalid states" and unordered_set
upper_bound With binary_function Visual Studio 2008 Bug?
Project
лаб1
Get parameter type
#26
Iterate_Variac_Template
throwing an exception containing a unique_ptr is dangerous
VS2013 hello world includes