I would like to understand all differences between operator new(std::size_t)
and new-expression
.
#include <iostream>
#include <new>
using std::cout;
struct A
{
int a;
char b;
A(){ cout << "A\n"; a = 5; b = 'a'; }
~A(){ cout << "~A\n"; }
};
A *a = (A*)operator new(sizeof(A)); //allocates 8 bytes and return void*
A *b = new A(); //allocates 8 bytes, invoke a default constructor and return A*
Could you provide all differences between them? Do they work in a different way?