I wrote a program that uses a stack ADT.
The main creates a new stack while giving 3 functions to use from the user:
Stack my_stack = sCreate (copy_int, free_int, print_int);
when I call to a 'peek' function:
printf ("Peek: |%d|\n\n", *(int*)sPeek(my_stack));
I have a memory leak.
the peek function looks like this:
Element sPeek (Stack stack){
if ((NULL == stack) || (0 >= stack->used_places))
return NULL;
Element returnElement = stack->copy_function(stack->stack_array[stack->used_places-1]);
if (NULL == returnElement){
free (returnElement);
return NULL;
}
return returnElement;
It's caused probably by the copy_function called there, which is the copy_int that was given by the user:
Element copy_int (Element element){
int *new_int = (int*) malloc(sizeof(int*));
*new_int = *(int*)element;
if (NULL != new_int)
return new_int;
else
return NULL;
How do I release the pointer (malloc) from copy_int?