std::malloc
is just a "renaming" of old C (not C++) function malloc(3).
So if it succeeds, it malloc(sizeof(T))
returns a pointer to an uninitialized memory zone of the size needed by T
You need to call some constructor of T
on that memory zone. You could use the placement new for that purpose, e.g:
void* p = std::malloc(sizeof(T));
if (!p) throw your_out_of_memory_exception();
T* ptr = new(p) T(32); /// placement new, with constructor called with 32
Actually many C++ implementations have their standard ::operator new
doing something similar. (So new
calls malloc
!)