Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It would seem that the size of a block referenced by a pointer returned by <strong>GetMem()</strong> must be available from somewhere, given that <strong>FreeMem()</strong> does not require that you identify the size of memory to be freed - the <em>system</em> must be able to determine that, so why not the application developer?</p> <p>But, as others have said, the precise details of the memory management involved are NOT defined by the system per se.... Delphi has always had a replaceable memory manager architecture, and the "interface" defined for compatible memory managers does not require that they provide this information for an arbitrary pointer.</p> <p>The default memory manager will maintain the necessary information in whatever way suits it, but some other memory manager will almost certainly use an entirely different, if superficially similar, mechanism, so even if you hack a solution based on intimate knowledge of one memory manager, if you change the memory manager (or if it is changed for you, e.g. by a change in thesystem defined, memory manager which you perhaps are using by default, as occurred between Delphi 2005 and 2006, for example) then your solution will almost certainly break.</p> <p>In general, it's not an unreasonable assumption on the part of the RTL/memory manager that the application should already know how big a piece of memory a <strong>GetMem()</strong> allocated pointer refers to, given that the application asked for it in the first place! :)</p> <p>And if your application <em>did NOT</em> allocate the pointer then your application's memory manager has absolutely no way of knowing how big the block it references may be. It may be a pointer into the <em>middle</em> of some larger block, for example - only the source of the pointer can possibly know how it relates to the memory it references!</p> <p>But, if your application really does need to maintain such information about it's <em>own</em> pointers, then it could of course easily devise a means to achieve this with a simple singleton class or function library through which <strong>GetMem()</strong>/<strong>FreeMem()</strong> requests are routed, to maintain a record of the associated requested size for each current allocated pointer. Such a mechanism could then of course easily expose this information as required, entirely reliably and independently of whatever memory manager is in use.</p> <p>This may in face be the only option if an "<em>accurate</em>" record is required , as a given memory manager implementation may allocate a larger block of memory for a given size of data than is actually requested. I do not know if any memory manager does in fact do this, but it could do so in theory, for efficiency sake.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload