Or will the memory be freed by the operating system independently?
#include <stdio.h> void main() { char *p = malloc(7); sprintf(p, "%s", "output"); puts(p); }
String is required
free(p);
in the above code before exiting main?
Or will the memory be freed by the operating system independently?
#include <stdio.h> void main() { char *p = malloc(7); sprintf(p, "%s", "output"); puts(p); }
String is required
free(p);
in the above code before exiting main?
All allocated memory should be freed immediately as it becomes unnecessary. It is your responsibility as a programmer.
Cleaning up resources for unloaded programs is the responsibility of the OS. And she didn’t care if this program ended successfully or not.
And you should not shift your work on other people's shoulders, although many do it :)
Yes. Memory (not shared) after the completion of the process is released.
However, a good style (all to clean yourself) should not be ignored.
Often, in practical programming, they use the "Copy / Paste" method. The code (often without much thought) is transferred from program to program. If the code originally written for main () falls into the library and will be called in a loop ...
Releasing resources should become a habit. This is easier. If you don’t like to free up memory each time, then switch to Java, or C #, or to any other language with the garbage collector.
Source: https://ru.stackoverflow.com/questions/10811/
All Articles