Will C automatically free memory without pointers?

Let's say we run this piece of code separately:

malloc(1024);

Will this lead to a memory leak, or will C automatically know how to free a pointer without references?

In other words, can I not assign it to a pointer?

void *p = malloc(1024);
free(p);
+2
source share
2 answers

In any code that you write that dynamically allocates memory, you have 2 responsibilities regarding any allocated memory block: (1) always keep a pointer to the start address for the memory block, so (2) it can be freed when it is no longer needed. Freeing up your memory is up to you.

, , , ( 1 ), - .

+4

. C ( , C ).

+5

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


All Articles