A memory-unsafe language like C is one in which programs can read and write to arbitrary memory. A memory-safe language is one where programs can read and write objects and in a pre-defined way.
There is a time and a place for both. You need to use memory-unsafe languages if you are writing device drivers and stuff where the point is the manipulate the machine.
For most application code, memory-safe languages should be used because they prevent bugs and you don’t need all that memory access.
Example
Let’s say we have a C program that has memory errors. We don’t want to debug it and we don’t want to rewrite it in a memory-safe language (which would remove these errors). What can we do? DieHard.
Memory safety usually incurs some runtime overhead to make sure shit isn’t being fucked up.
Selecting a Language
- What kinds of errors the language admits or prevents
- The imagined runtime performance
- The convenience of expressing the required computation
- The computing environment (including available libraries)
- The skills of the available programmers
The latter two are extrinsic to the language. Very few programs actually need to perform memory unsafe operations. Everything else should behave in a memory safe way.
An argument against using a memory-safe language is the incurred cost of the runtime needing to ensure memory safety. However this is a stupid argument because:
- Practical economics: The major costs of producing software are testing and maintenance
- Theoretical economics: Take advantage of economies of scale by using memory safe languages devs have poured time into making
- Parallel performance: Performance gains are negligible unless they are due to parallelization
- Memory hierarchy is the bottleneck: The actual bottleneck of performance is the CPU needing to sit idle while data comes in from main memory.
- Performance is empirical: You don’t realllly know one is better than the other unless you do both because only then will you know where the performance problems are.
Other than systems programming, when does it make sense to forego memory safety for performance?
- No dynamic memory allocation is required
- The program is not interactive (batch-mode) and short lived
- The program implements a single well understood algorithm
- The program does not accept input