Difficulties malloc and free

I have two related questions, so I ask them in this single thread.

Q1) How can I confirm that my OS automatically frees up “free” memory (allocated with malloc) automatically when the program exits? I am using Ubuntu 11.04, 32-bit with gcc-4.5.2

According to Stephen Summit's study page here , “Free unused memory (malloc'ed) is a good idea, but it is not mandatory. When your program terminates, any memory that it allocated but did not free should be automatically released. If your computer somehow "lost" memory just because your program forgot to free it, it would mean a problem or a lack of your operating system . "

Q2) Suppose foo.c modifies the memory of B-bytes. Later, foo.c frees these B-byte memory cells and returns them to the OS. Now my question is , can these DEFINED B-bytes of memory locations be redistributed to foo.c (by the operating system) in the current instance, or can these B-bytes not be allocated to foo.c until its current instance terminates?

EDIT . I would recommend to anyone who reads my question to read the answer to a similar question here and. Both answers explain the interaction and operation of malloc () and free () with good granularity without the use of very esoteric terms. To understand the DIFFERENCE between the memory management tools used by the kernel (e.g. brk (), mmap ()) and those used by the C compiler (e.g. malloc (), free ()), this is MUST BE READ.

+3
source share
4 answers

, , SIGSEGV _ exit (2) ( main), . , , ( mmap (2) (, , sbrk(2))) > ).

, free () malloc (, ), , , , munmap(2) .

1234, /proc/1234/maps ( /proc/self/maps ). /proc . ( /proc/self/statm /proc/self/smaps ).

free malloc . malloc free , malloc , (.. C + ) , .

valgrind . Boehm, .. GC_malloc malloc .

+2

, .
, , , .

( ), .

+2

Q1. , .

Q2. , foo.c, , .

+1

Q1) I am not sure how you can confirm. However, in the second paragraph, he believed that good style always frees up any memory that you allocate. A good explanation of this can be found here: What REALLY happens when you do not release after malloc? .

Q2) Definitely; these bytes are usually first redistributed (depending on the malloc implementation). For a detailed explanation, see How do malloc () and free () work? .

+1
source

All Articles