On 12/16/2010 11:38 AM, Ered China Luin wrote:
> In article<iedert$6f8$>,
> Eric Sosman<> wrote:
>
>> On 12/16/2010 10:54 AM, Ered China Luin wrote:
>>> [...]
>>> (2) Write your own allocator in front of malloc. Perhaps something like
>>>
>>> static char empty = 0;
>>> void *allocate(int n) {return n==0 ?&empty : malloc(n);}
>>> void dispose(void *p) {if (p!=&empty) free(p);}
>>
>> You can do this in your own program if you like, but note that
>> malloc(0) itself cannot work this way. If it returns non-NULL, it
>> must return a value that is distinct from all the other values it
>
> And you can change it to
> void *allocate(int n) {return malloc(n==0 ? 1 : n);}
That works. It's silly, IMHO, but it works. (Keep in mind that
`int' and `size_t' are not synonymous, so you may be inviting trouble
with your choice of parameter type.)
>> Of course, you can make your wrapper behave as you please -- but
>> you can't change the behavior of the underlying malloc().
>
> And I can hide that malloc if I want.
Not sure what you mean by "hide." You cannot "hide" it in the
sense of making it unavailable to other code in the program, nor can
you "hide" it in the sense of intercepting all malloc() calls and
routing them somewhere other than to the real malloc(). You can, of
course, threaten to flog or "hide" or "give a hiding to" anyone who
calls malloc() directly instead of using your wrapper -- but that's
social engineering, not software engineering.
--
Eric Sosman
lid