Why is my pointer not null after free?

后端 未结 6 1965
执笔经年
执笔经年 2020-12-08 17:17
void getFree(void *ptr)
{
    if(ptr != NULL)
    {
        free(ptr);
        ptr = NULL;
    }
    return;
}
int main()
{
char *a;
a=malloc(10);
getFree(a);
if(a==         


        
6条回答
  •  没有蜡笔的小新
    2020-12-08 17:37

    You are passing the pointer By value.. (By default C passes the argument by value) which means you are updating the copy only ..not the real location..for that you might need to use pointer to pointer in C

    void getFree(void **ptr)
    {
    
        if(*ptr != NULL)
        {
            free(*ptr);
            *ptr = NULL;
        }
    
        return;
    }
    

提交回复
热议问题