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
#7
Wide string conversion with multibyte chars and locale
Get parameter type
VS struct name enumerate
C++ Assignment..
new
Integral constant
operator/function lookup
Time Zone Registry
hangman