I am learning c++ and read why we can't use realloc() in std::vector, but i think we can use realloc() for pods, so here is my attempt to write std::vector like class template specialized for PODs
#include <malloc.h>
#include <stdexcept>
#include <type_traits>
namespace gupta {
namespace detail {
namespace podvector {
template <typename T> inline auto malloc(size_t elm_count) {
return ::malloc(elm_count * sizeof(T));
}
template <typename T> inline auto realloc(void *old_block, size_t elm_count) {
return ::realloc(old_block, elm_count * sizeof(T));
}
} // namespace podvector
} // namespace detail
template <typename PodType,
typename = std::enable_if_t<std::is_pod<PodType>::value>>
class podvector {
public:
using value_type = PodType;
using size_type = size_t;
using pointer = value_type *;
using const_value_pointer = const value_type *;
~podvector() {
if (m_memory)
free(m_memory);
}
podvector(size_type initial_size = 0) {
alloc(initial_size);
m_size = initial_size;
}
podvector(size_type initial_size, const value_type &value)
: podvector(initial_size) {
for (auto &v : *this)
v = value;
}
podvector(const podvector &other) : podvector(other.m_size) {
for (size_type i = 0; i < m_size; i++)
m_memory[i] = other.m_memory[i];
}
podvector(const podvector &&other)
: m_memory{std::move(other.m_memory)},
m_capacity{std::move(other.m_capacity)}, m_size{
std::move(other.m_size)} {
// other.m_memory = nullptr;
other.alloc(0);
}
podvector &operator=(const podvector &rhs) {
if (this != &rhs) {
resize(rhs.m_size);
for (size_type i = 0; i < rhs.m_size; i++)
m_memory[i] = rhs[i];
}
return *this;
}
podvector &operator=(podvector &&rhs) {
if (this != &rhs) {
this->~podvector();
m_memory = std::move(rhs.m_memory);
m_capacity = std::move(rhs.m_capacity);
m_size = std::move(rhs.m_size);
// rhs.m_memory = nullptr;
rhs.alloc(0);
}
return *this;
}
void resize(size_type new_size) {
if (new_size > m_size)
change_capacity(new_size);
m_size = new_size;
}
void reserve(size_type new_capacity) {
if (m_capacity < new_capacity)
change_capacity(new_capacity);
}
void push_back(const value_type &new_elm) {
if (m_size + 1 > m_capacity)
change_capacity(std::min(m_capacity * 2, 1));
m_memory[m_size++] = new_elm;
}
void pop_back() { m_size--; }
auto size() const { return m_size; }
auto capacity() const { return m_capacity; }
pointer begin() { return m_memory; }
const_value_pointer begin() const { return m_memory; }
pointer end() { return m_memory + m_size; }
const_value_pointer end() const { return m_memory + m_size; }
value_type &operator[](size_type pos) { return m_memory[pos]; }
const value_type &operator[](size_type pos) const { return m_memory[pos]; }
private:
pointer m_memory;
size_type m_size, m_capacity;
void alloc(size_type capacity) {
m_capacity = capacity;
m_size = 0;
m_memory =
static_cast<pointer>(detail::podvector::malloc<value_type>(capacity));
if (!m_memory)
throw std::bad_alloc{};
}
void change_capacity(size_type new_capacity) {
m_capacity = new_capacity;
void *new_memory =
detail::podvector::realloc<value_type>(m_memory, new_capacity);
if (!new_memory)
throw std::bad_alloc{};
m_memory = static_cast<pointer>(new_memory);
}
};
} // namespace gupta
2 Answers 2
mallocandrealloclive incstdlib.malloc.his not a standard header, neither in C++ nor in C, and should not be relied on in any case.- Also, you're missing
#include <utility>forstd::move. - Legacy C functionality, as everything else from the standard library, lives in the
stdnamespace, if you include the corresponding<cheader>files. While you can also include the C standard headers via<header.h>, this is highly discouraged. With the first option, names may also exist in the global namespace, but this is not guaranteed and should not be relied on. - Your
mallocandreallocfunctions need not beinline. In fact,inlinedoes nothing here; remove it. - You don't need to check for null pointers when
freeing memory. Passing a null pointer tofreeis always well defined and does nothing. - You don't need to move out all the data members from
otherin your move constructor. All of those members are trivial to copy, and have no special move behavior. - The move assignment operator invokes undefined behavior. You cannot just call the destructor of an object and then write to its members. Calling the destructor completely ends the lifetime of the object; afterwards, there's nothing there to write to. Furthermore, this is a kind of anti-pattern; in almost all cases, the move assignment operator should be implemented via the copy-and-swap idiom.
push_backis broken. You callchange_capacitywith the result ofstd::min(m_capacity * 2, 1), which is either 0 or 1, in any case. Most likely, you'd want to usestd::maxinstead.if (m_size + 1 > m_capacity)looks strange to my eyes. I would just writeif (m_size >= m_capacity).pop_backshould definitely return a value. It's not very useful otherwise.- Since you're going for what seems to be a std-like container interface, you should add a little more functionality. For one, you're missing some of the usual type definitions, such as
iteratorandconst_iterator. Furthermore, some sort ofemplacemechanism would be appropriate, as well as some way to clear the container and some functionality to shrink it, as well as some other things. - You should make sure that memory is properly initialized. As a user, I would expect that it's safe to access every element in bounds; however, you are not initializing memory at all, leaving that job to the user. I'd expect memory to be default- or zero-initialized; making use of
std::callocshould do the trick (alternatively, you can also juststd::memsetthe area). - I would make your
mallocandreallocfunctions a little bit more useful. As of right now, the onus of checking whether allocation succeeded is put on the user of those functions, but in my opinion it would be much cleaner and safer to lift those functions to a "C++-safe" level, i.e. make them check the return value of the underlying allocation call directly and convert that into an exception directly. std::is_podis being deprecated, in accordance with the standard library moving away from the term POD as a whole. Also, the requirement thatTmust be a POD type is too strict; your vector will still work correctly ifTis trivial.
-
\$\begingroup\$ Re #10:
std::vector::pop_backdoesn't return a value, either. What would be nice, however, would be aback()member function that returns a reference to the last element in thepodvector. \$\endgroup\$hoffmale– hoffmale2018年07月29日 12:56:30 +00:00Commented Jul 29, 2018 at 12:56 -
\$\begingroup\$ @hoffmale You're right. Still, why not fix the mistakes of the standard library while you're at it :P \$\endgroup\$Ben Steffan– Ben Steffan2018年07月29日 13:05:51 +00:00Commented Jul 29, 2018 at 13:05
-
\$\begingroup\$ Actually, I'm siding with the standard library: Removing the last element is a different operation than retrieving it (so it violates SRP), and if it isn't used by the caller
pop_backjust performs an unnecessary copy. \$\endgroup\$hoffmale– hoffmale2018年07月29日 13:55:45 +00:00Commented Jul 29, 2018 at 13:55 -
\$\begingroup\$ @hoffmale Well, in that case, the name
pop_backis just badly chosen. A pop operation should always return the popped value. If that's your stance, you should advocate for renaming the function toremove_back, or something along those lines. \$\endgroup\$Ben Steffan– Ben Steffan2018年07月29日 15:17:31 +00:00Commented Jul 29, 2018 at 15:17 -
\$\begingroup\$ If any
pop-like member function were to return a value, no container could provide the strong exception guarantee mandated by the standard. This might not matter if the language is garbage collected and/or uses references only (basically forcing everybody to something similar tostd::vector<std::unique_ptr<T>>, where the assignment isnoexcept), but this option is not available in C++. What would you do ifTcontains some resource (e.g. it's owning a pointer or file handle), but the assignment of the result frompop(or the like) throws an exception for some reason? \$\endgroup\$hoffmale– hoffmale2018年07月29日 16:09:48 +00:00Commented Jul 29, 2018 at 16:09
Memory Allocation Layer
Before looking at everything lets look at the memory allocation layer.
#include <malloc.h>
namespace gupta {
namespace detail {
namespace podvector {
template <typename T> inline auto malloc(size_t elm_count) {
return ::malloc(elm_count * sizeof(T));
}
template <typename T> inline auto realloc(void *old_block, size_t elm_count) {
return ::realloc(old_block, elm_count * sizeof(T));
}
} // namespace podvector
} // namespace detail
....
private:
void alloc(size_type capacity) {
m_capacity = capacity;
m_size = 0;
m_memory =
static_cast<pointer>(detail::podvector::malloc<value_type>(capacity));
if (!m_memory)
throw std::bad_alloc{};
}
void change_capacity(size_type new_capacity) {
m_capacity = new_capacity;
void *new_memory =
detail::podvector::realloc<value_type>(m_memory, new_capacity);
if (!new_memory)
throw std::bad_alloc{};
m_memory = static_cast<pointer>(new_memory);
}
Lets just say that <malloc.h> is not a valid header. You should probably be using <cstdlib> (this is C++ so prefer the C++ headers) which puts all the function in the standard namespace. So your use of ::malloc() and ::realloc() are wrong as they are not necessarily in the global namespace (you just happen to get lucky).
You do the memory allocation in your gupta::detail::podvector namespace but leave the checking for null to your vector class. Using Separation of Concerns we should really be doing all the memory handling (including checking for failed allocation) in your memory functions not inside your vector.
Also why are you static_cast the result in the vector. Would it not be easier to cast the type in the allocation routines so that you return the correct type.
Though your use of static_cast is correct this is the kind of thing I would want other developers to have a critical look at to make sure I got it correct. Thus I would have used reinterpret_cast<>() to make people stop and think to make sure cast is correct.
There is a bug in your current code. A std::vector always makes sure all members are in a specific state after initialization. You don't do this. Your main constructor simply calls alloc() then sets the size. Since alloc simply calls std::malloc() the underlying memory is not in any specific state. Thus I would use std::calloc() to make sure the memory returned is in a standard state.
template <typename T>
T* new_malloc(size_t elm_count) {
auto tmp = std::calloc(elm_count, sizeof(T));
if (tmp == nullptr) {
throw std::bad_alloc("PLOP");
}
return reinterpret_cast<T*>(tmp);
}
template <typename T>
T* new_realloc(void *old_block, size_t elm_count) {
auto tmp = std::realloc(old_block, elm_count * sizeof(T));
if (tmp == nullptr) {
throw std::bad_alloc("PLIP");
}
return reinterpret_cast<T*>(tmp);
}
...
private:
void alloc(size_type capacity) {
m_capacity = capacity;
m_size = 0;
m_memory = detail::podvector::new_malloc<value_type>(capacity));
}
void change_capacity(size_type new_capacity) {
m_capacity = new_capacity;
m_memory = detail::podvector::new_realloc<value_type>(m_memory, new_capacity);
}
Specialization for POD
If you want to specialize for POD. You don't need to customize the whole vector. You just need to specialize the allocation method used by the vector.
You do need to keep it in mind for a couple of functions (but I don't think you need to do anything special).
private:
// POD Data
typename std::enable_if_t<std::is_pod<PodType>::value>>::value_type
alloc(size_type capacity) {
m_capacity = capacity;
m_size = 0;
m_memory = detail::podvector::new_malloc<value_type>(capacity));
}
typename std::enable_if_t<std::is_pod<PodType>::value>>::value_type
change_capacity(size_type new_capacity) {
m_capacity = new_capacity;
m_memory = detail::podvector::new_realloc<value_type>(m_memory, new_capacity);
}
// Any other Data
typename std::enable_if_t<!std::is_pod<PodType>::value>>::value_type
alloc(size_type capacity) {
m_capacity = capacity;
m_size = 0;
m_memory = ::operator new(sizeof(T) * m_capacity);
}
typename std::enable_if_t<!std::is_pod<PodType>::value>>::value_type
change_capacity(size_type new_capacity) {
podvector<T> tmpBuffer(newCapacity);
for(loop = 0; loop < m_size; ++loop) {
tmpBuffer.push_back(std::move(m_memory[loop]));
}
tmpBuffer.swap(*this);
}
-
\$\begingroup\$ I'm more in favor of using
if constexprfor the specializations, makes the code easier to read \$\endgroup\$JVApen– JVApen2018年08月02日 18:00:09 +00:00Commented Aug 2, 2018 at 18:00 -
\$\begingroup\$ @JVApen Do you have a link to an example or description? \$\endgroup\$Loki Astari– Loki Astari2018年08月02日 18:25:39 +00:00Commented Aug 2, 2018 at 18:25
-
\$\begingroup\$ stackoverflow.com/a/51659883/2466431 should indicate the idea \$\endgroup\$JVApen– JVApen2018年08月02日 18:27:41 +00:00Commented Aug 2, 2018 at 18:27
-
\$\begingroup\$ @JVApen I have seen that once before. But I have never used it. I will need to experiment. I have some code reviews coming up where this may help. \$\endgroup\$Loki Astari– Loki Astari2018年08月02日 18:30:37 +00:00Commented Aug 2, 2018 at 18:30