mallocandrealloclive incstdlib.malloc.his not a Cstandard header, neither in C++ nor in C, and should not be included from a C++ programrelied 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.::malloc, if you include the corresponding::realloc<cheader>, files. While you can also include the C standard headers via::size_t<header.h>etc, this is highly discouraged. With the first option, names may also exist in the global namespace, but that'sthis 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.
Corrected several points in response to @Deduplicator's comment. Added a new point to address the pod vs. trivial issue.
mallocandrealloclive incstdlib.malloc.his a C header and should not be included from a C++ program.- Also, you're missing
#include <utility>forstd::move. - Legacy C functionality, as everything else from the standard library, lives in the
stdnamespace.::malloc,::realloc,::size_tetc. may also exist in the global namespace, but that's 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.
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.
mallocandrealloclive incstdlib.malloc.his a C header and should not be included from a C++ program.- Also, you're missing
#include <utility>forstd::move. - Legacy C functionality, as everything else from the standard library, lives in the
stdnamespace.::malloc,::realloc,::size_tetc. may also exist in the global namespace, but that's 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.
lang-cpp