Re: Malcolm's new book - Chapter 1 review
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 23 Aug 2007 19:24:22 +0100
Ed Jensen wrote, On 23/08/07 15:10:
Flash Gordon <spam@xxxxxxxxxxxxxxxxxx> wrote:Wouldn't simply switching to calloc() solve that problem, since itNot 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.
allocates memory and writes to all of it?
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
.
- References:
- Re: Malcolm's new book - Chapter 1 review
- From: Eric Sosman
- Re: Malcolm's new book - Chapter 1 review
- From: Malcolm McLean
- Re: Malcolm's new book - Chapter 1 review
- From: Richard Heathfield
- Re: Malcolm's new book - Chapter 1 review
- From: pete
- Re: Malcolm's new book - Chapter 1 review
- From: Richard Heathfield
- Re: Malcolm's new book - Chapter 1 review
- From: Philip Potter
- Re: Malcolm's new book - Chapter 1 review
- From: Richard Heathfield
- Re: Malcolm's new book - Chapter 1 review
- From: Malcolm McLean
- Re: Malcolm's new book - Chapter 1 review
- From: CBFalconer
- Re: Malcolm's new book - Chapter 1 review
- From: Keith Thompson
- Re: Malcolm's new book - Chapter 1 review
- From: Ed Jensen
- Re: Malcolm's new book - Chapter 1 review
- From: Flash Gordon
- Re: Malcolm's new book - Chapter 1 review
- From: Ed Jensen
- Re: Malcolm's new book - Chapter 1 review
- Prev by Date: Re: Malcolm's new book - Chapter 1 review
- Next by Date: Satellite TV on your PC without Paying Monthly FEES
- Previous by thread: Re: Malcolm's new book - Chapter 1 review
- Next by thread: Re: Malcolm's new book - Chapter 1 review
- Index(es):
Relevant Pages
|