It's UWAweek 30 (2nd semester, week 1)

help2002

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 6 other people reading this forum.


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

Login to reply

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

ANONYMOUS wrote:
> 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

    free(p);
}

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  7:24AM Jul 25 2024
Privacy policy