I am making a program for managing stocks (for fun and to learn)
Here's some bits of the code you need to know in order to help me:
Code:
struct stockNode {
int ID;
struct stockNode *next;
};
typedef struct stockNode stock;
int memLoc(void **var, int size)
{
if (NULL == (*var = malloc(size)))
{
printf("\nError (sui). Unable to allocate memory.");
system("pause");
return -1;
}
else return 1;
}
This is a function, with which the program finds the given stock by ID. head is already allocated in the previous function and passed on by address.
Code:
void menu_findStock(stock **head)
{
stock *curr; memLoc(&(curr), sizeof(stock));
curr = *head;
...
curr = NULL;
free(curr);
}
As you can see, I allocate memory for curr in the function and then I assign curr = *head, which assigns the pointer curr to point to the address of *head as I believe. The thing is, if I do free(curr) at this point, I lose *head and all its values. Why?
So I tried setting curr = NULL, but is using free(curr) after that of any use?
Thanks.