Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Dynamically sized array at end of struct
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 #include <iostream> #include <Windows.h> struct MyStructPtr { UINT src; UINT dst; UINT* pRects; }; struct MyStructArr { UINT src; UINT dst; UINT pRects[1]; }; int main() { MyStructPtr ptr = { }; MyStructArr arr = { }; // This causes the app to crash // ptr.pRects[0] = 1; arr.pRects[0] = 1; // This causes the app to crash // std::cout << ptr.pRects[0] << std::endl; std::cout << arr.pRects[0] << std::endl; const UINT sizePlusThreeSlots = sizeof(MyStructArr) + sizeof(UINT) * 3; MyStructArr* pArr = reinterpret_cast<MyStructArr*>(malloc(sizePlusThreeSlots)); if (pArr != nullptr) { pArr->pRects[0] = 1; pArr->pRects[1] = 2; pArr->pRects[2] = 3; pArr->pRects[3] = 4; std::cout << pArr->pRects[0] << std::endl; std::cout << pArr->pRects[1] << std::endl; std::cout << pArr->pRects[2] << std::endl; std::cout << pArr->pRects[3] << std::endl; free(pArr); } }
run
|
edit
|
history
|
help
-1
Fast sine to fill array (sin/cos pair)
div64 inline asm
Chord Note Finder
123
timeConversion
Computing factorial of an integer with recursion and iteration [EDIT]
variable template not supported
hangman
virtual test
infix to postfix v 3.0