[fpc-devel] Support needed in understanding some heap.inc related stuff

Nikolay Nikolov nickysn at users.sourceforge.net
Sun Sep 28 18:35:21 CEST 2014

On 09/28/2014 05:45 PM, Lag Programming wrote:
>    Hi! I need some help in understanding three things viewed in heap.inc.
> ...
> 3) I'm not interested in the importance of the presented function. Why 
> the "existing" code design is preferred over "alternative" code?
> Existing code:
> function SysAllocMem(size: ptruint): pointer;
> begin
>   result := MemoryManager.GetMem(size);
>   if result<>nil then
>     FillChar(result^,MemoryManager.MemSize(result),0);
> end;
> Alternative code:
> function SysAllocMem(size: ptruint): pointer;
> begin
> result:=MemoryManager.GetMem(size);
> if result<>nil then FillChar(result^,size,0);
> end;
> The only things I could find to support the "existing" code over 
> "alternative" code are that(now or in the future):
> a) Maybe there is a possibility that the value of variable size gets 
> changed after "result:=MemoryManager.GetMem(size);"
This cannot happen, because the 'size' parameter to MemoryManager.GetMem 
is passed by value, not by reference.
> b) Maybe there is a possibility that following 
> "result:=MemoryManager.GetMem(size);" variable result would point to a 
> memory block that has allocated a size different than variable 
> "size"'s content.
This could happen, at least in theory. Memory managers usually align 
memory block size up to some given granularity, so they often allocate 
more memory than you requested. The question is whether they save and 
return the original size in MemSize or the aligned (i.e. bigger) one. If 
they store the aligned size, it's a good idea, just in case, to clear 
all the memory up to that size.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20140928/3db44d20/attachment.html>

More information about the fpc-devel mailing list