fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. class A {
  7. public:
  8. int v;
  9. A() {
  10. std::cout << "запуск конструктора ... " << std::endl;
  11. v = 10; //0xA
  12. }
  13. };
  14.  
  15. int main() {
  16. // без вызова конструктора -------------------------------
  17. char *a = (char*)malloc(1024);
  18. A *x = (A*)a;
  19. *a++ = 255; // 0xFF
  20. *a = 128; // 0x80
  21. std::cout << "x->v: " << std::setbase(16) << x->v << std::endl;
  22. free(--a);
  23. // с вызовом конструктора --------------------------------
  24. A *y = new A();
  25. std::cout << "y->v: " << y->v << std::endl;
  26. delete x;
  27. // -------------------------------------------------------
  28. return 0;
  29. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
x->v: 80ff
запуск конструктора ... 
y->v: a