fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct A {
  5. int i;
  6. };
  7.  
  8. int main(int argc, char* argv[]) {
  9. A a{ 5 }; //Constructs an A object on the stack
  10. A* b = new A{ 7 }; //Constructs an A object on the heap and stores a pointer to it in b
  11. A* c = new A[] { //Construct an array of A objects on the heap and stores a pointer to it in c
  12. { 3 },
  13. { 4 },
  14. { 5 },
  15. { 6 }
  16. };
  17. std::cout << "a: " << a.i << "\n"; //Prints 'a: 5'
  18. std::cout << "b: " << b->i << "\n"; //Prints 'b: 7'
  19. std::cout << "c: " << c[0].i << "; " << c[1].i << "; " << c[2].i << "; " << c[3].i << "\n";
  20. //Prints 'c: -33686019; -1414812757; -1414812757; -1414812757'
  21.  
  22. delete b;
  23. delete[] c;
  24. return 0;
  25. }
Compilation error #stdin compilation error #stdout 0s 3460KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main(int, char**)':
prog.cpp:11:18: error: expected primary-expression before ']' token
     A* c = new A[] { //Construct an array of A objects on the heap and stores a pointer to it in c
                  ^
prog.cpp:16:5: error: too many initializers for 'A [1]'
     };
     ^
stdout
Standard output is empty