Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory corruption

i was running a small c program:

#include<stdio.h>
int main()
{
char *p;
p = (char *)malloc(10);
free(p);
free(p);
free(p);
printf("\npointer is freed!!\n");
}

basically i am freeing the memory which has already been freed. i think should result in a core dump!!is it not so?

but it is printing the

pointer is freed!!

am i wrong some where?

like image 854
Vijay Avatar asked May 21 '26 04:05

Vijay


1 Answers

As per the man page, "if free(ptr) has already been called before, undefined behavior occurs."

It doesn't need to blow up; "not doing anything" is perfectly acceptable undefined behaviour. Also are nasal demons. Don't rely on it.

like image 177
Tordek Avatar answered May 23 '26 21:05

Tordek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!