Skip to content

Commit fc37289

Browse files
committed
fix: Prevent segmentation fault at Simple generic Stack
1 parent e5dad3f commit fc37289

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

data_structures/stack/stack.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,8 @@ void grow()
4545
{
4646
max += 10; /* increases the capacity */
4747

48-
int i; // for the loop
49-
void **tmp = malloc(sizeof(void *) * max);
50-
51-
/* copies the elements from the origin array in the new one. */
52-
for (i = 0; i < max - 10; i++)
53-
{
54-
*(tmp + i) = *(array + i);
55-
}
56-
/*free the memory */
57-
free(array);
58-
array = tmp;
48+
array = (void **)realloc(array, sizeof(void *) * max);
49+
assert(array); /* tests whether pointer is assigned to memory. */
5950
}
6051

6152
/* push: pushs the argument onto the stack */

0 commit comments

Comments
 (0)