3 ms·
I assure you that "malloc" and "free" don't "provide the illusion of infinite memory". Quite the opposite, in fact.
by theseoafs 10y ago
I assure you that "malloc" and "free" don't "provide the illusion of infinite memory". Quite the opposite, in fact.
- stcredzero 10y agoChallenge: Write a Turing complete machine using a finite number of registers and a state machine. One or all of the registers can contain a rational number of unlimited precision. (This has already been done, so if you are aware of the existing machines, you have to create a new one.)
- Ericson2314 10y agoThis...is not possible?
- rntz 10y agoNo, it's possible. The key insight is that a "rational number of unlimited precision" can be used to store an arbitrarily large amount of data; it can emulate a Turing machine's unlimited working tape.
- stcredzero 10y agoYou win a stcredzero "no-prize!"
- pjmlp 10y agoThey do if always matched 1:1, it doesn't usually happen, specially in big codebases, thus leading to CVEs.
- theseoafs 10y agoThey surely do not. Malloc is specified in such a way such that the request for memory can fail (which leads to returning NULL).
- rntz 10y agoThe specification allows for this, yes. However, on some platforms (including linux glibc by default, I believe), malloc() never fails, but allocates virtual memory optimistically; the first you hear of an out of memory condition is when the system slows down due to paging, and the next thing you notice is when the OOM killer nixes a process. Of course, other platforms, especially embedded ones, behave differently.
- amag 10y agoActually there is one reason for malloc to return NULL even with virtual memory, your process can run out of address space.
- roganp 10y agoOr run out of room in the paging file. Your addressable memory cannot be larger than physical memory without a backing store.
- prodigal_erik 10y agoDepending on VM_OVERCOMMIT_MEMORY, Linux might give out address space well beyond the size of the pagefile, hoping many of those pages are never written to (e.g., most threads never get anywhere near the bottom of their default stacks).
- pjmlp 10y agoAssuming that the OS APIs used by malloc() do tell the application about OOM.
- MaulingMonkey 10y agoIn practice, many programs ignore the fact that malloc can return NULL. As do some OSes and their implementations of malloc, if they support/enable/require overcommit. These are perhaps operating under "the illusion of infinite memory" (in the GC sense): free, in this context, is simply a way of marking data as invalid and no longer to be referenced - a method of poisoning data for debug purposes. But of course, I've had malloc return NULL - very finite.