fork download
  1. #include <iostream>
  2.  
  3. struct S
  4. {
  5. S(int i) : i(i) {}
  6. void print() const { std::cout << i << std::endl; }
  7. int i;
  8. };
  9.  
  10. int main() {
  11.  
  12. const int cnt = 10;
  13. void* p = ::operator new(sizeof(S)*cnt);
  14. for(int i=0;i<cnt;++i)
  15. new (p+i*sizeof(S)) S(i);
  16.  
  17. S* s = static_cast<S*>(p);
  18.  
  19. for(int i=0;i<cnt;++i)
  20. s[i].print();
  21.  
  22. // S* ss = new S[cnt]; // не работает без конструктора по умолчанию
  23. return 0;
  24. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
0
1
2
3
4
5
6
7
8
9