New answers tagged

1

I don't think these can be negative: int64_t count; int64_t cntrl_count; Consider an unsigned type (perhaps size_t is most appropriate?) This cast and associated use are not portable: create_shared_ptr((void**)&v,(void(*)(void*))cleanup); p->cntrl->cleanup(p->cntrl->data); Although function pointers can be cast to another ...


2

Tolerate NULL during clean-up As free(NULL) is OK, consider a NULL test for clean-up void cleanup_shared_ptr(shared_ptr_t * p){ if (p) { .... } } Lack of documentation *.h files deserve some overall comments. Consider users may not to wade through the .c file code to deduce functionality. Fixed width I see not compelling reasons for int64_t here. ...


2

Your implementations for "insert" and "remove" are very inefficient. First, there is no obvious reason why "remove" would reallocate the array. And there is no reason why "insert" would always reallocate the array. Here's what I would do: Add a private method where you pass the intended capacity. If the intended ...


7

Const Correctness A member function that doesn't modify the underlying object should normally be a const member function. For example: T get(int index); //O(1) This doesn't modify the contents of the DynamicArray, and returns the element by value (not reference) so client code can't get access to the DynamicArray's internal data to modify it either. As ...


9

delete looks strange. There is no reason to create new_arr. Everything can (and should) be done in place. BTW, m_capacity = m_capacity * 2; there is just wrong (I suspect a copy-paste from insert). if in a tight loop incurs a strong performance penalty. Consider breaking the insertion loop into two: for (int i = 0,; i < index; i++) { new_arr[i] = ...


16

Welcome to C++, and welcome to Code Review. C++ memory management is, as you probably have realized, tough and error-prone. There are many things that can easily go wrong. Assuming that no exception is thrown, I don't see obvious memory leaks in your code; however, there are still some issues worth discussing. You can take a look at my implementation of a ...


Top 50 recent answers are included