Here is implementation of stack with previous memory allocation