Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a double free in C?

Tags:

c

free

Normally, if a pointer is freed twice, it's a double free. For example,

char *ptr;
ptr=malloc(5 * sizeof(*ptr));
free(ptr);
free(ptr);

The above code is considered as double free. Is the following considered as double free as well?

char *ptr;
char *ptr1;
ptr=malloc(5 * sizeof(*ptr));
ptr1=ptr;
free(ptr);
free(ptr1);

Thank you.

like image 201
biajee Avatar asked Dec 01 '22 10:12

biajee


1 Answers

Yes. The library doesn't care what name you gave a varaible in your source code (it's long gone by the time the code is executed). All that matters is the value, and in this case the values passed to free() would be the same.

like image 130
James Curran Avatar answered Dec 15 '22 05:12

James Curran