I am working on an application where I am allocating memory from a pool for use with arithmetic data types - all fundamentals, except for std::complex. I have been using malloc, ensuring alignment, and casting the pointer to the data type without any issues. I know placement new is, in general, required for doing this with objects. However, I am not clear if this is required for fundamental data types, and if there are any exceptions to needing this with objects. Specifically, is this legal to do with std::complex?
//Simple example just using malloc
Class AnyClass;
AnyClass *p = malloc(sizeof(AnyClass));
//We have memory allocated for the size of AnyClass, but no object at that memory. Construct an object a location p using placement new
AnyClass *o = new (p) AnyClass;
//Then o can be used. To cleanup:
o->~AnyClass();
free(p);
//In C, we can just do the following. Is this legal in C++ (with fundamental types)?
int *p = malloc(sizeof(int));
//Can just use p as a pointer to int now. Cleanup is just:
free(p);
//If the omitting a call to placement new is legal with fundamental data types in C++, are there ANY objects where this is legal? If so, what exactly are the requirements? Is this ok with std::complex?