Why should i use pointer to pointer in my saferFree(void**) function when i want to free memory inside that function? Why below code does'n free ip pointer?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void saferFree(void *);
int main()
{
int *ip;
ip = (int*) malloc(sizeof(int));
*ip = 5;
saferFree(ip);
return 0;
}
void saferFree(void *pp){
if((void*)pp != NULL){
free(pp);
pp = NULL;
}
}
Above code doesn't free pp pointer but below code with double pointer works properly.I want to know Why?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void saferFree(void **);
int main()
{
int *ip;
ip = (int*) malloc(sizeof(int));
*ip = 5;
saferFree(&ip);
printf("%p", ip);
return 0;
}
void saferFree(void **pp){
if(*pp != NULL){
free(*pp);
*pp = NULL;
}
}