This is C. Malloc is "memory allocation" and it will essentially "reserve" a certain amount of memory to store data (an integer in this case) and return the adress of that allocated memory (known as pointer). So now you can point at that adress and read a value or write a new one.
Free is the reverse, it frees the memory space so now it can be used by something else in the program. In a lot of languages all memory is freed automatically after the program stops executing, but not C. If you don't free the memory other programs can't use it. (It gets freed eventually in modern computers tough)
Yes any memory allocated to a program is automatically returned to the operating system when the program exit and stop running.
However malloced memory who aint got a free in a server program will continue to increase the memory heap until the program is terminated with out of memory since all available memory has been used by the program. But that is just for the program.
Operating systems usually do not have that problem. Well, most operating systems do not have that problem. For example Windows 95 was prone to that error.
1
u/LostgamerFJ 2d ago
I'm not good enough at programming for this. What do the "free" and "malloc" functions do?