- // since a stack is a single-linked list, an auxiliary storage is required
- void** tmpStorage = (void**) malloc(stack->size * sizeof (void*));
- StackCell* stackCell = stack->top;
- for (UInt i = 0; i < stack->size; i++)
- {
- tmpStorage[stack->size - 1 - i] = stackCell->data;
- stackCell = stackCell->previous;
- }
- // now transfer tmpStorage contents (pushed in right order) in a new stack
- Stack* stackCopy = _stack_new(stack->dataSize);
- for (UInt i = 0; i < stack->size; i++)
- _stack_push(stackCopy, tmpStorage[i]);
- free(tmpStorage);
+ Stack* stackCopy = (Stack*) safe_malloc(sizeof (Stack));
+ stackCopy->dataSize = stack->dataSize;
+ Vector* arrayCopy = vector_copy(stack->array);
+ stackCopy->array = arrayCopy;