Remember that parameters in C ++ are passed by value. You assign a resized copy of the pointer that was passed to you, the pointer outside the function remains the same.
You should either use a double indirect (or double pointer), i.e. a pointer to a pointer to an int ):
void ResizeArray(int **orig, int size) { int *resized = new int[size * 2]; for (int i = 0; i < size; i ++) resized[i] = (*orig)[i]; delete [] *orig; *orig = resized; }
or link to a pointer:
void ResizeArray(int *&orig, int size) { int *resized = new int[size * 2]; for (int i = 0; i < size; i ++) resized[i] = orig[i]; delete [] orig; orig = resized; }
By the way, for array sizes you should use the type std::size_t from <cstddef> - it is guaranteed to keep the size for any object and makes it clear that we are dealing with the size of the object.
source share