Easy enough. This is a somewhat common issue for new programmers. Look at the code:
node *temp = malloc(sizeof(node));
temp = cursor;
This code is declaring a pointer called temp, initializing it with a malloc (meaning that memory is being allocated), and then reassigning a different address from another pointer, thus losing the address of the memory that was just allocated. This is the leak.
Note that declaration and initialization of a variable are two separate and distinct processes. A var can be declared without being initialized, although that's a bad practice. There are two "good practice" methods to use here. First, declare the var and initialize it with a null:
node *temp = NULL;
This declares and initializes it without allocating any memory. It also tells the system that it doesn't contain a real address. It points at nothing. If you just did
node *temp; it would be uninitialized and would contain whatever garbage data that was in memory at that time. The consequences of that could be disastrous. (A test for NULL would fail, it could contain an invalid address that would throw a runtime error, or worse, it could contain random data that matches a valid address allocated to the current program with unpredictable results!)
However, since you're immediately reassigning the contents, why not do it more efficiently?
node *temp = cursor;
The important lesson here is that when you declare a pointer, you're not REQUIRED to malloc memory to it, but you should always initialize it somehow so that it doesn't contain garbage data. ;-)
Of course, I haven't examined whether the function is working correctly, but that's a different question! ;-D
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)