Re: Malcolm's new book - Chapter 1 review



Ed Jensen wrote, On 23/08/07 15:10:
Flash Gordon <spam@xxxxxxxxxxxxxxxxxx> wrote:
Wouldn't simply switching to calloc() solve that problem, since it
allocates memory and writes to all of it?
Not necessarily. It is common for the OS to provided zeroed memory so calloc will not have to write to it if the memory has been freshly obtained from the OS, which is when there is a potential problem.

That's interesting.

I just realised that what I wrote could be misinterpreted. It is common for the OS to provide zeroed memory, but the reason has nothing to do with calloc it is to prevent you getting possibly sensitive data from some other program. Given that this occurs malloc could be written as:
IF memory available in free list THEN
zero memory and return pointer to it
ELSE
Get memory from OS and return pointer to it without zeroing it

> Would malloc() followed by memset() (to manually
zero the entire contents of the allocated memory) resolve the problem?

Depends. The compiler could be clever enough to replace that with a call to calloc.

I assume you would need some OS-specific code to catch a failed
memset() (assuming the "real" allocation happens during the write, and
you'd need to catch the failure somehow).

Since you have to go the system specific route, you might as well go the system specific route of finding a way to disable lazy allocation.

Also, calloc does not resize blocks, and although it is not obvious from the quoted material the original discussion was about growing buffers using realloc.

That's also interesting. I've never tried to realloc() memory
allocated with calloc() before, and thus never gave it any thought.

You can, although any extra memory will not be zeroed obviously.

The original discussion was about a buffer allocated with malloc then grown with realloc (no zeroing of memory involved). Someone suggested using calloc to sidestep the lazy allocation problem.
--
Flash Gordon
.



Relevant Pages

  • Re: Calloc arguments order
    ... I would like to understand something about the Calloc function. ... The callocfunction allocates memory for an array of nmemb elements ... because in practice Calloc will be implemented by a Malloc ... the allocation is only large enough for 952 objects, ...
    (comp.lang.c)
  • Re: How to release heap memory that is marked as free
    ... As I said, fragmentation is a very serious problem, and one of the most serious problems ... my allocator was accused of using massive amounts of memory. ... I'm going to have to re-think the memory allocation that I'm ... process's 'working set'. ...
    (microsoft.public.vc.mfc)
  • Re: [PATCH 00/28] Swap over NFS -v16
    ... memory they can consume. ... So we need the extra (skb) ... included in the reserve? ... if the allocation had to dip into emergency reserves, ...
    (Linux-Kernel)
  • Re: Memory leak with CAsyncSocket::Create
    ... read my essay on how storage allocators work. ... Create method is consuming system memory that is not released back to ... The memory consumption is either shown as "Mem Usage" on the Task ... many levels of allocation going ...
    (microsoft.public.vc.mfc)
  • Re: OT: C++ overloading operators
    ... dynamic allocation, no matter how many "clever tricks" are used... ... though there's enough memory in the system, ... all these "flexible data types" map into CPU command ... The computing environment is completely ...
    (comp.dsp)