I am writing a dynamic array that contains pointers of values in each index. I'm trying to write a takeAt method, but I'm not sure if it causes a memory leak.
Pointer array is a list T** list = new T* [SIZE];
T** list = new T* [SIZE];
Excerpt:
template <class T> const T& List<T>::takeAt(const int &index) { if (isEmpty()) { return T(); } assert(index >= 0 && index < size); T* ptr = list[index]; int numMove = size - index; memmove(list + index, list + index + 1, numMove * POINTER_SIZE); list[size] = NULL; size--; return *ptr; }
If this causes a memory leak, how can I fix it (without returning a pointer instead)? Please pay attention to something else that I am doing wrong. Thanks in advance.
, , . , , , , . const , (ala std::vector::at ), . , ( ?).
std::vector::at
, , . - :
template <class T> T* List<T>::takeAt(const int index) { if (isEmpty()) { return NULL; } ... return ptr; }
NULL, , , , ( , ). , , , , ? .
: , int by const. , .
, , ; , . , -, .
, , . , .
int index = 4; T &value = list_var.takeAt(index); // ... delete &value;
, , T . - . , .
, NULL . , NULL . .
, , , .
, , , , . - , , .
, . , , , . , , .
, "". ++, std:: list.