fork(1) download
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. typedef unsigned int DWORD;
  6. typedef void VOID;
  7. typedef wchar_t WCHAR;
  8.  
  9. struct _POWER_BROADCAST
  10. {
  11. DWORD Message;
  12. DWORD Flags;
  13. DWORD Length;
  14. WCHAR ptr[1];
  15. };
  16.  
  17. struct _POWER_BROADCAST_POWER_INFO
  18. {
  19. int data;
  20. };
  21.  
  22. int main()
  23. {
  24. // 重點是這裡配置的長度
  25. _POWER_BROADCAST* realObject =
  26. (_POWER_BROADCAST*)malloc(sizeof(_POWER_BROADCAST) + sizeof(_POWER_BROADCAST_POWER_INFO));
  27.  
  28. realObject->Message = 7788;
  29. realObject->Length = 123;
  30.  
  31. _POWER_BROADCAST_POWER_INFO *Data =
  32. (_POWER_BROADCAST_POWER_INFO *)realObject->ptr;
  33. Data->data = 5566;
  34.  
  35. //先印出message, 成功
  36. cout<<realObject->Message<<endl;
  37.  
  38. _POWER_BROADCAST_POWER_INFO* ptr2 =
  39. (_POWER_BROADCAST_POWER_INFO*)realObject->ptr;
  40.  
  41. cout<<ptr2->data;
  42. }
  43.  
Success #stdin #stdout 0.02s 2856KB
stdin
Standard input is empty
stdout
7788
5566