Why is my pointer not null after free?

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

    The question has already been answered but if it helps, I can explain it graphically.

    You are doing this --> the pointer is copied by value to your function so it points to the array

    but instead you want this -->point to the original pointer

    As Merlyn Morgan-Graham already said, the way to solve it is to add operators * and &.

提交回复
热议问题