Does fortran have a garbage collector (gc)?

I recently talked to someone who said he was programming Fortran (from the way back), but he could not tell me if Fortran has a garbage collector. He told me that he did not use malloc or for free in Fortran, so I assume that he has a garbage collector? Or does fortran have no garbage collector and just a memory leak that will be recovered by the operating system when the program ends? I do not know anything about Fortran, except that it was used back. I also tried a quick Google search, but could not find anything fast.

+4
source share
2 answers

Modern Fortran has many ways to declare variables. Elements declared simply will exist throughout the life of the object. Thus, "real, dimension (N) :: array" declared in the procedure will automatically disappear when this procedure returns. Naturally, variables declared in the main program or module variables or general (obsolete) will be stored throughout the program.

Variables can be dynamically allocated using "allocate" (for this they must be declared using the allocatable attribute). Because Fortran 95 distributed variables that are local to the procedure are automatically freed when the procedure returns! They will not leak memory! (Some programmers may find it good practice to explicitly free variables anyway, even if this is not strictly necessary.) (Of course, you can waste memory in the sense that you do not explicitly free a variable that you know needs more.)

Possible memory leak by pointers. You can allocate memory with a pointer, and then assign a pointer to another variable, losing the previous link. If you do not free memory, you have a leak. There is less need for pointers in Fortran than in some other languages ​​... much can be done with distributed variables that are safer - there are no memory leaks.

Related questions: Fortran> resource retention time and ALLOCATABLE arrays or POINTER arrays?

+18
source

No, Fortran does not have a garbage collector. However, in this case there is an additional package for the F90 . No, I have not used it.

+2
source

All Articles