Q:

C equivalent of new Object(constructor)

What's the C equivalent (with malloc) of the following
MyClass* ptr = new MyClass(arg1,arg2);

The first thing that comes to my mind is to instantiate a MyClass object on the stack, then do a malloc(sizeof(MyClass)); and then a memcpy(stack, to_malloc'd_memory)
Is there a better way to do it?

A:

The equivalent (or replacement) of a constructor in C is an initializer. You can use such an initializer in the construction of a compound literal (another C speciality) and by that initialize your new object by assignment.
MyClass* ptr = malloc(sizeof *ptr);
*ptr = (MyClass){ .a = 1, .b = 34 };

a convention to do so systematically could be to always have an "init" function
inline
MyClass* MyClass_init(MyClass* ptr, T1 arg1, T2 arg2) {
   if (ptr) {
     *ptr = (MyClass){ .a = arg1, .b = arg2, };
   }
   return ptr;
}

and then to call that at initialization of the pointer
MyClass* ptr = MyClass_init(malloc(sizeof *ptr), arg1,arg2);

