malloc() behavior (was: Pointer please)

Mark Terribile materribile at yahoo.com
Mon Oct 6 09:42:30 PDT 2003



>>>> It does not matter what freebsd does, C does
>>>> not require that malloc initialize space
>>>> according to Kernighan and Ritchie.
>> ... What's really bad, is that freebsd could
>> potentally change there behavor down the line.
>> Its probably dictated by the way kernel dezined,
>. meaning they may do whats the cheapist.

> There's nothing bad about it.  FreeBSD follows
> the standards.  ...

There's a distinction here which has been mentioned
and perhaps lost.  The kernel does provide programs
with zero'd memory, but unless you are doing system
calls (man section 2) directly you are not seeing
what the kernel does.  You are seeing what the C
language runtime code (malloc(), calloc(), free(),
etc.) does.  They use the system calls, but the
semantics are the semantics that the authors of
the runtime implemented.  And since the runtime is
provided with the compiler (typically gcc) it's
only a function of convenience when the language
definition says it may be.

The kernel's pool of zero'd buffer pages is used to
provide zero'd memory on demand while doing the work
when the CPU may be free.  Sort of like washing the
dishes before you need them.  The kernel C code
does not use the C runtime, except possibly for some
very low-level routines that might be needed to
implement extended precision, do stack frame
management, or other very-low-level stuff.  And
there's precious little needed on newer processors.

                                  Mark Terribile


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com


More information about the freebsd-questions mailing list