简体   繁体   中英

Crouton environment does not free memory after exiting program?

I am new to using Crouton. I am trying to use it for some C programming practice in a linux environment. Anytime I run a program that uses malloc(), I get a memory leak error for x amount of bytes. The memory leak error dissipates when I explicitly free the allocated memory. Is this a Crouton issue or a Ubuntu issue overall?

Is there any way to fix it so that I do not have to explicitly free allocated space every time? I understand the free()ing is good coding practice and whatnot, but I was just wondering if there was a way so that allocated memory is automatically free'd after exiting.

On Linux (and most modern multi-tasked OSes) when a process is exiting (or is terminated, eg by a signal) all its resources are released by the OS kernel, including its virtual address space .

So it is certainly possible to forget to free heap memory before exiting, and many (but not all) programs are doing so.

If you are developing an application, you might still want to properly free every dynamically allocated memory zone when exiting. This facilitates the use of memory leak detecting tools like valgrind (but take some development efforts, and even some run time).

C is a Manual memory management language, it means you need to release memories that you have allocated manually in contrast with Garbage collection . You may use functions such as atexit() to facilitate memory deallocation process at program exit, but I don't suggest this personally as a good programming practice. If you are programming with C programming language, you should learn to deallocate memory with free() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM