Skip to main content
88 votes
Accepted

Why don't compilers automatically insert deallocations?

Because it's undecidable whether the program will use the memory again. This means that no algorithm can correctly determine when to call free() in all cases, which ...
David Richerby's user avatar
59 votes

Why don't compilers automatically insert deallocations?

As David Richerby rightly noted, the problem is undecidable in general. Object liveness is a global property of the program, and may in general depend on the inputs to the program. Even precise ...
Pseudonym's user avatar
  • 22.3k
30 votes

Why don't compilers automatically insert deallocations?

It's an incompleteness problem, not an undecidability problem While it's true that the optimal placement of deallocation statements is undecidable, that's simply not the issue here. Since it's ...
Nat's user avatar
  • 1,351
24 votes

Why don't compilers automatically insert deallocations?

Currently, none of the posted answers are fully correct. It's not impossible to do this, but adding that feature would restrict mermory allocation patterns. Why don't compilers automatically insert ...
Paul Draper's user avatar
10 votes

Why don't compilers automatically insert deallocations?

"Humans do it, so it's not impossible" is a well-known fallacy. We do not necessarily understand (let alone control) the things that we create - money is a common example. We tend to overestimate (...
André Souza Lemos's user avatar
9 votes
Accepted

Could a compiler automatically insert memory deallocations if it were to simulate every conditional statement?

No, it's not possible for a compiler to do that. Doing so is an undecidable problem. To get some intuition for why it is hard, consider the combinations of loops and conditional statements. It's not ...
D.W.'s user avatar
  • 162k
9 votes

Why don't compilers automatically insert deallocations?

The lack of automatic memory management is a feature of the language. C is not supposed to be a tool for writing software easily. It is a tool for making the computer do whatever you tell it to do. ...
Jouni Sirén's user avatar
9 votes

Why don't compilers automatically insert deallocations?

The issue is mostly a historic artifact, not an impossibility of implementation. The way most C compilers build code is so that the compiler only sees each source file at a time; it never sees the ...
Grumbel's user avatar
  • 207
6 votes

Why don't compilers automatically insert deallocations?

Other answers have focussed on whether it is possible to do garbage collection, some details of how it's done, and some of the problems. One issue which hasn't yet been covered though is the ...
Graham's user avatar
  • 219
5 votes

Could a compiler automatically insert memory deallocations if it were to simulate every conditional statement?

What D.W. said is correct in one way: It is impossible for a compiler in general to decide that at point X in a program, object O is not referenced anymore and can be deallocated. However, you can (...
gnasher729's user avatar
  • 30.7k
4 votes

Why don't compilers automatically insert deallocations?

The question presumes a deallocation is something the programmer is supposed to deduce from other parts of the source code. It's not. "At this point in the program, memory reference FOO isn't useful ...
Travis Wilson's user avatar
4 votes

Why don't compilers automatically insert deallocations?

What is done: There is garbage collection, and there are compilers using reference counting (Objective-C, Swift). Those that do reference counting need help from the programmer by avoiding strong ...
gnasher729's user avatar
  • 30.7k
4 votes
Accepted

Is it possible to implement a WeakMap with primitive keys and weak values?

Okay. My confusion with this question is that I assumed that what you wanted is a weak value map, which it appears is what you want, and this can be implemented in a straightforward manner (at least ...
Derek Elkins left SE's user avatar
3 votes

Could a compiler automatically insert memory deallocations if it were to simulate every conditional statement?

... if a compiler would simulate every conditional statement in a program ... You forget about programs that have some input: Let's say we have two variables: A ...
Martin Rosenau's user avatar
3 votes

Could a compiler automatically insert memory deallocations if it were to simulate every conditional statement?

For a more practical example, imagine writing a program that reads in a graph, performs some operations on it, and prints it out. It's possible a command in the middle deletes a node, dropping half ...
prosfilaes's user avatar
3 votes

Why don't compilers automatically insert deallocations?

In languages like C, the programmer is expected to insert calls to free. Why doesn't the compiler do this automatically? Because the lifetime of a memory block is the programmer's decision, not the ...
Agent_L's user avatar
  • 213
3 votes

What are some possible "functional" memory structures?

The topic is fairly broad. Functional hardware has been explored in e.g. Lisp machines. More on the theoretical side, you may want to read about the various abstract machines which have been ...
chi's user avatar
  • 14.6k
2 votes

Why is the object destructor paradigm in garbage collected languages pervasively absent?

Why is the object destructor paradigm in garbage collected languages pervasively absent? I come from a C++ background, so this area is baffling to me. The destructor in C++ actually does two things ...
Pacerier's user avatar
  • 123
2 votes

Why is the object destructor paradigm in garbage collected languages pervasively absent?

When the of two most popular GC frameworks (Java and .NET) were being designed, I think the authors expected that finalization would work well enough to avoid the need for other forms of resource ...
supercat's user avatar
  • 1,261
2 votes

Why does garbage collection extend only to memory and not other resource types?

All memory is equal, if I ask for 1K, I don’t care where in the address space the 1K comes from. When I ask for a file handle, I want a handle to the file I wish to open. Having a file handle open ...
Ian Ringrose's user avatar
2 votes

delete node from linked list (lang w/GC) -should deleted item 'next' be set to null

If you mean the next pointer of the node that was deleted, as far as I know it is normally not necessary to set the pointer to null. Once the node is deleted, if the code is not buggy, then the code ...
D.W.'s user avatar
  • 162k
1 vote

Is it possible to implement a WeakMap with primitive keys and weak values?

It would be pointless to have a primitive as the key in a Javascript WeakMap, as you can't/don't garbage-collect primitives, and a WeakMap is entirely about whether or not GC will garbage-collect the ...
D.W.'s user avatar
  • 162k
1 vote
Accepted

What would a language look like in which precise GC was implementable as a library?

I believe that this is possible, or at least nearly possible, in a language like Rust, though perhaps not necessarily in the sense you are thinking. Rust actually has a GC library, though I can't say ...
Joey Eremondi's user avatar
1 vote

What would a language look like in which precise GC was implementable as a library?

I think it is possible to implement a garbage collector in C++ without changing the language itself. But in order to use the garbage collector, one must restrict the programmer from using arbitrary ...
Cecilia Chen's user avatar
1 vote

Is reference counting GC vs. tracing GC a language property or an implementation property?

You could take the language known as Swift and rename it to "Swift with ARC". You could then create a new language named "Swift with GC" with exactly the same syntax, but with fewer guarantees about ...
gnasher729's user avatar
  • 30.7k
1 vote
Accepted

How did MacLisp's garbage collector "run in the register set"?

The 16 high-speed registers of the PDP-10 ISA were addressible as locations 0-15 of physical memory, so that the code literally could run in the registers if it was short enough and you could devote a ...
John Cowan's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible