Hi I have a "myfree" function which frees my pointer allocated memory space, but after freeing my pointers and make them NULL, when the functions returns to main the pointer address (although already free) it still points to the previous address?
can someone explain me why this is happening, it seems to me like a problem of "passing by value" but I'm "passing by reference" to avoid this issues isn't it?
Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct S1{
int a;
char b;
}S1;
void myfree(void* freeptr)
{
if (!freeptr){
return;
}
printf("%s-About to free %p\n",__func__,freeptr);
free(freeptr);
freeptr = NULL; # this line do not do what I want when it returns to main
printf("%s-Freed %p\n",__func__,freeptr);
}
int main()
{
S1 *ps1 = NULL;
ps1 = malloc(sizeof(S1));
printf("%s-About to free %p\n",__func__,ps1);
myfree(ps1);
printf("%s-Freed %p\n",__func__,ps1);
return 0;
}
And this the output of the program:
main-About to free 0x9cdf378
myfree-About to free 0x9cdf378
myfree-Freed (nil)
main-Freed 0x9cdf378 <--------- I was expecting this to be NULL ?!?!?!?!
Thanks for your help!