Why is my pointer not null after free?

后端 未结 6 1933
执笔经年
执笔经年 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:55

    Because the getFree() function takes a copy of the pointer. ptr and c are both pointers, but they are different variables. It's the same reason why this function will output "6":

    void Magic(int x)
    {
        x = 1;
    }
    
    void main()
    {
        int a = 6;
        Magic(a);
        printf("%d", a);
    }
    

提交回复
热议问题