Jump to content
dummzeuch

GetMemory vs. GetMem

Recommended Posts

The Online Help says the following about GetMemory:

Quote

 

GetMemory allocates a memory block.

GetMemory allocates a block of the given Size on the heap, and returns the address of this memory. The bytes of the allocated buffer are not set to zero. To dispose of the buffer, use FreeMemory. If there is not enough memory available to allocate the block, an EOutOfMemory exception is raised.

If the memory needs to be zero-initialized, you can use AllocMem.

Note: GetMemory is the C++ compatible version of GetMem.

http://docwiki.embarcadero.com/Libraries/en/System.GetMemory

 

What exactly does it mean by "GetMemory is the C++ compatible version of GetMem."? What does GetMemory do that GetMem doesn't?

Why and when should I use GetMemory or GetMem?

 

This is the OLH for GetMem:

Quote

GetMem allocates a memory block.

GetMem allocates a block of the given Size on the heap, and returns the address of this memory in the P parameter. The bytes of the allocated buffer are not set to zero. To dispose of the buffer, use FreeMem. If there is not enough memory available to allocate the block, an EOutOfMemory exception is raised.

Note: If the memory block must be initialized with zero, you can use AllocMem.

This function is not available in C++. In C++ you can use GetMemory.

In case of a typed pointer, you should consider the New and Dispose procedures, which respectively initialize and finalize the memory block accordingly.

I can't see any difference.

 

Maybe it's just that one is available for C++ Builder programs, and the other isn't? So for Delphi programs it doesn't matter which to use?

Edited by dummzeuch

Share this post


Link to post
function _GetMem(Size: NativeInt): Pointer;
begin
  if Size <= 0 then
    Exit(nil);
  Result := MemoryManager.GetMem(Size);
  if Result = nil then
    Error(reOutOfMemory);
end;
function GetMemory(Size: NativeInt): Pointer; cdecl;
begin
  Result := MemoryManager.GetMem(Size);
end;

Do you see the difference?

 

Also GetMem is an intrinsic that maps to System._GetMem 

Edited by Stefan Glienke

Share this post


Link to post

So the description for GetMemory in the online help is wrong: It does not raise an EOutOfMemory exception.

 

Share this post


Link to post
2 hours ago, dummzeuch said:

What exactly does it mean by "GetMemory is the C++ compatible version of GetMem."?

I'd read that as "GetMemory exists so that the Crtl unit can implement malloc". 

Edited by David Heffernan

Share this post


Link to post
6 hours ago, David Heffernan said:

I'd read that as "GetMemory exists so that the Crtl unit can implement malloc". 

But actually, that turns out to be completely wrong.  At least in XE7 malloc is implemented as

 

function  malloc(size: size_t): Pointer; cdecl;
begin
  Result := AllocMem(size);
end;

 

which is pretty lame because AllocMem is zeroising the memory. And potentially throwing an exception. So, a somewhat bogus attempt to implement malloc. 

 

Perhaps what is really going on is that GetMemory is the function that you need to call if you want to allow GetMemory(0) to return a pointer to a block of memory, ie. that GetMemory(0) <> nil.  But I'm not convinced by that either since the C standard demands that malloc(0) returns a value that cannot be dereferenced, which may or may not be null.

 

Or perhaps it is just a function that returns nil in case of error rather than raising an exception.

Edited by David Heffernan

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×