Can you print the line after I freed it?

I study and test memory allocation in C, and I want to check what happens if free() called.

I was expecting a segmentation error or a NULL pointer to occur after running the program below. However, I can still successfully print the line as in Output. I also tried to free str twice, then an error occurred with exit 2.

It seems that the previously allocated memory was successfully freed, but the data in the memory is not cleared. It's right? If so, when will the program clear the freed memory space? Is it safe if the data is freed but not cleared?

Answers to any question above will be helpful! Thanks!

Code

 #include <stdio.h> #include <stdlib.h> int main() { printf("Hello, World!\n"); char *str = (char *)malloc(24); char *str2 = "tutorialspoint"; strcpy(str, str2); free(str); printf("[str] %s\n", str); return 0; } 

Output

 Hello, World! [str] tutorialspoint 

Output 2

 main(83218,0x7fff9d2aa3c0) malloc: *** error for object 0x7fb0b2d00000: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Abort trap: 6 

Edit

Thanks to everyone for the helpful answers. Now I understand that in C there are some undefined behaviors (UB), and this helped me to understand, something else confused me, for example, writing a line outside the selected area (for example, in the code snippet below). This caused UB according to the wiki , but the program will not work.

Feel free to correct me if I am wrong!

 char *str = (char *)malloc(0); char *str2 = "tutorialspoint"; strcat(str, str2); printf("[str] %s, [addr] %p\n", str, str); 
+5
source share
2 answers

You need to learn the concept of undefined behavior.

Even if you print a line after it is released and it "works", this does not mean that it worked. This is just one of the things that can happen when a behavior is called undefined , which in this case is.

Also, the pointer will not be NULL , unless you do this

 free(ptr); ptr = NULL; 

and free() , does not set all bytes to 0 .

The free() function does exactly what it calls. This allows malloc() to reuse the free() d fragment. The data pointed to by the pointer still remains unchanged, and for this reason you can print the previous content after free() . But there is absolutely no guarantee what will happen if you search for a pointer after free() or if it still exists or is completely or partially rewritten.

If you do dereferencing a pointer after free() d it, there is one thing that you know is undefined behavior.

+6
source

When you call free , it allows you to use directed memory for other purposes. Attempting to read or write freed memory causes undefined behavior .

Just because a memory has been discarded does not necessarily mean that it has been physically erased.

Analogy: Suppose you left a book in your hotel room. You are staying at a hotel, but you still have a key. If you return to the room, the book may be there, or it may not be so.

+4
source

Source: https://habr.com/ru/post/1269995/


All Articles