From 5f138cc0700434aefb1d9c8ded8ab5d41a58062f Mon Sep 17 00:00:00 2001 From: Hert Zhao Date: Thu, 11 Jul 2019 00:20:32 +0800 Subject: [PATCH] fix the stack.c --- data_structures/stack/stack.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/data_structures/stack/stack.c b/data_structures/stack/stack.c index 29c6a1de7..a3ebb266d 100644 --- a/data_structures/stack/stack.c +++ b/data_structures/stack/stack.c @@ -42,7 +42,7 @@ void initStack() grow: increases the stack by 10 elements. This utility function isn't part of the public interface */ -void grow() +void grow(void ** oldMemory) { max += 10; /* increases the capacity */ @@ -52,10 +52,11 @@ void grow() /* copies the elements from the origin array in the new one. */ for (i = 0; i < max - 10; i++) { - *(tmp + i) = *(array + i); + *(tmp + i) = *(oldMemory + i); } - - array = tmp; /* setups the new one as basis */ + /*free the memory */ + free(oldMemory); + array = tmp; } /* push: pushs the argument onto the stack */ @@ -81,7 +82,7 @@ void push(void *object) else /* stack is full */ { - grow(); /* lets grow stack */ + grow(array); /* lets grow stack */ push(object); /* recursive call */ } }