I'm currently working through Zed Shaw's Learn C the Hard Way tutorials and I'm trying to understand what is on the stack and what is on the heap in the following example, and how free() is working.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
/** our old friend die from ex17. */
void die(const char *message)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n",message);
}
exit(1);
}
// a typedef creates a fake type,
// in this case for a function pointer
typedef int (*compare_cb)(int a, int b);
// by declaring this pointer to a function
// we can use this like an int/char etc. in a function's arguments
/**
* A classic bubble sort function that uses the
* compare_cb to do the sorting.
*/
int *bubble_sort(int *numbers, int count, compare_cb cmp)
{
int temp = 0;
int i = 0;
int j = 0;
int *target = malloc(count * sizeof(int));
if(!target) die("Memory error.");
memcpy(target, numbers, count * sizeof(int));
for(i = 0; i < count; i++) {
for(j = 0; j < count - 1; j++){
if(cmp(target[j], target[j+1]) > 0) {
temp = target[j+1];
target[j+1] = target[j];
target[j] = temp;
}
}
}
return target; // returns address of target(I think)
} // target will persist even after function
// exit because we created on the heap (memcopy)
// so what happens if we run more than once?
int sorted_order(int a, int b)
{
return a - b;
}
int reverse_order(int a, int b)
{
return b - a;
}
int strange_order(int a, int b)
{
if(a == 0 || b == 0) {
return 0;
} else {
return a % b;
}
}
/**
* used to test that we are sorting things correctly
* by doing the sort and printing it out.
*/
void test_sorting(int *numbers, int count, compare_cb cmp)
{
int i = 0;
int *sorted = bubble_sort(numbers, count, cmp);
if(!sorted) die("failed to sort as requested.");
for(i = 0; i < count; i++){
printf("%d ", sorted[i]);
}
printf("\n");
free(sorted);
sorted = NULL;
}
int main(int argc, char *argv[])
{
if(argc < 2) die("USAGE: ex18 4 3 1 5 6");
int count = argc - 1;
int i = 0;
char **inputs = argv + 1;
int *numbers = malloc(count * sizeof(int));
if(!numbers) die("Memory error.");
for(i = 0; i < count; i++) {
numbers[i] = atoi(inputs[i]);
}
test_sorting(numbers, count, sorted_order);
test_sorting(numbers, count, reverse_order);
test_sorting(numbers, count, strange_order);
free(numbers);
numbers = NULL;
return 0;
}
In the function bubble_sort
, an array of ints target
is created on the heap. My understanding is that since this is on the heap, it will persist after the function exits.
int *target = malloc(count * sizeof(int));
the function then returns target
return target;
I believe that means that the function returns the address of target
later, in test_sorting
the result of the bubble_sort
function is passed
int *sorted = bubble_sort(numbers, count, cmp);
so, if I'm right, the pointer sorted
has been set to the same address as target
at the end of test_sorting
*sorted
is freed but the data pointed to by *target
is never freed.
But when I run the program in Valgrind I get no memory leaks, so this can't be true.
Am I right, then, in saying that when I free a pointer, the thing it points to is freed? I think probably not... I can't find any reference to this online so I'm assuming I'm mistaken at some point above, but I can't see where.