<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 09/28/2014 05:45 PM, Lag Programming
wrote:<br>
</div>
<blockquote
cite="mid:8D1A941EDC01749-1438-2A27F@webmail-va026.sysops.aol.com"
type="cite"><font color="black" face="arial" size="2"><font
size="2"> Hi! I need some help in understanding three things
viewed in heap.inc.<br>
<br>
...<br>
3) I'm not interested in the importance of the presented
function. Why the "existing" code design is preferred over
"alternative" code?<br>
<br>
Existing code:<br>
function SysAllocMem(size: ptruint): pointer;<br>
begin<br>
result := MemoryManager.GetMem(size);<br>
if result<>nil then<br>
FillChar(result^,MemoryManager.MemSize(result),0);<br>
end;<br>
<br>
Alternative code:<br>
function SysAllocMem(size: ptruint): pointer;<br>
begin<br>
result:=MemoryManager.GetMem(size);<br>
if result<>nil then FillChar(result^,size,0);<br>
end;<br>
<br>
The only things I could find to support the "existing" code
over "alternative" code are that(now or in the future):<br>
a) Maybe there is a possibility that the value of variable
size gets changed after "result:=MemoryManager.GetMem(size);"<br>
</font></font></blockquote>
<font size="2"><font face="arial">This cannot happen, because the
'size' parameter to MemoryManager.GetMem is passed by value, not
by reference.</font></font><br>
<blockquote
cite="mid:8D1A941EDC01749-1438-2A27F@webmail-va026.sysops.aol.com"
type="cite"><font color="black" face="arial" size="2"><font
size="2">
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.<br>
</font></font></blockquote>
<font size="2"><font face="arial">This could happen, at least in
theory</font></font>. 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.<br>
<br>
Nikolay<br>
</body>
</html>