Option A:
T * address = static_cast<T *>(::operator new(capacity * sizeof(T), std::nothrow));
Option B:
T * address = static_cast<T *>(std::malloc(capacity * sizeof(T)));
Context:
template <typename T>
T * allocate(size_t const capacity) {
if (!capacity) {
throw some_exception;
}
//T * address = static_cast<T *>(std::malloc(capacity * sizeof(T)));
//T * address = static_cast<T *>(::operator new(capacity * sizeof(T), std::nothrow));
if (!address) {
throw some_exception;
}
return address;
}
std::malloc
is shorter, but ::operator new
is clearly C++ and it's probably implemented in terms of std::malloc
. Which one is better / more idiomatic to use in C++.