It's UWAweek 29 (mid-year break)


This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.
Displaying selected article
Showing 1 of 1168 articles.
Currently 1 other person reading this forum.

 UWA week 40 (2nd semester, week 10) ↓
SVG not supported

Login to reply

5:29am Tue 3rd Oct, Christopher M.

> Do we need to free absolutely everything that we've malloc'd after the program finishes syncing and is about to exit? It's my understanding that when a process terminates, it's memory is freed automatically so it's sort of unneeded unless you wanted to extend the program, even if it might be bad practice.
Your understanding is correct, a terminating process's memory is returned to the OS for other processes (existing and new) to use. So any allocated memory use in/for long-term data structures need not be freed. However, as a general practice, you should try to free memory that you only require for a short time. For example, if you allocate memory at the top of a loop, and use it only within the loop, then you should free it at the bottom of the loop, rather than letting it become disconnected garbage in your program:
for(int i=0 ; ....) {
    char *p = malloc(...);

    // use p's memory


The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  5:07AM Sep 06 2023
Privacy policy