The difference between New and Getmem

Source: Internet
Author: User

If you manipulate a string variable in a record pointer, you will lose the inside of the string
Storage space, causing a memory leak?

The result: the memory allocated with New () automatically initializes the contents of the record and automatically
Clears all allocated memory, including the memory of a string or other dynamic array. Getmem/freemem didn't have this.
Properties. In fact, Getmem is called in New () and some initialization operations are performed.

The code is as follows:

Type
Pmyrecord = ^tmyrecord;
Tmyrecord = Record
I:integer;
s:string;
V:variant;
End

{; $DEFINE NEW}

Procedure Tform1.button1click (Sender:tobject);
Var
R:pmyrecord;
I:integer;
Begin
For I: = 1 to 1024x768 do
Begin
{$IFDEF NEW}
New (R); Initialize the R.S correctly
SetLength (R.S, $FFFF);
Dispose (R); Properly freeing R.S memory space
{$ELSE}
Getmem (R, SizeOf (Tmyrecord));
R.S: = "; Error
SetLength (R.S, $FFFF);
Freemem (R);
{$ENDIF}
End
End

===================================================

Getmem is only responsible for allocating space and will not be responsible for emptying the space just allocated, if it needs to be allocated
Empty space can be used Allocmem,allocmem = Getmem + Fillchar

===================================================

Oh, I make a mistake above, think S = 0 o'clock also can error, so no Fillchar, actually not.

This will correctly measure the result:
(You can see the memory footprint of two methods from Task Manager by $DEFINE NEW;

Type
Pmyrecord = ^tmyrecord;
Tmyrecord = Record
I:integer;
s:string;
V:variant;
End

{; $DEFINE NEW}

Procedure Tform1.button1click (Sender:tobject);
Var
R:pmyrecord;
I:integer;
Begin
For I: = 1 to 1024x768 do
Begin
{$IFDEF NEW}
New (R); Initialize the R.S correctly
SetLength (R.S, $FFFF);
Dispose (R); Properly freeing R.S memory space
{$ELSE}
Getmem (R, SizeOf (Tmyrecord));
Fillchar (r^, SizeOf (Tmyrecord), #0);
SetLength (R.S, $FFFF);
Freemem (R); Does not release R.S memory space!!
{$ENDIF}
End
End


===================================================

Yes, Freemem does not release content that is automatically managed in its lifetime because the Freemem
It seems that those are consistent binary data, with no meaning whatsoever. But you can pass a
Finalize calls (or Finalizerecord) to resolve the problem I estimate that Dispose is directly
or indirectly called the Finalize

===================================================

Oh, no estimate, help is clearly explained.

In Delphi code, FREEMEM destroys the variable referenced by P and returns their memory to the heap. If P does not the memory in the heap, a runtime error occurs. If P points to a structure this includes long strings, variants, dynamic arrays, or interfaces, call Finalize before calli Ng Freemem.
......
Note:it is preferable to use the New and Dispose procedures rather than getmem and Freemem. When using the New and Dispose, there is no need to explicitly call Finalize.

===================================================
Guttier wrote:
Oh, no estimate, help is clearly explained.

In Delphi code, FREEMEM destroys the variable referenced by P and returns their memory to the heap. If P does not the memory in the heap, a runtime error occurs. If P points to a structure this includes long strings, variants, dynamic arrays, or interfaces, call Finalize before calli Ng Freemem.
......
Note:it is preferable to use the New and Dispose procedures rather than getmem and Freemem. When using the New and Dispose, there is no need to explicitly call Finalize.


I'll translate this paragraph in English.

"In Delphi code, Freemem frees memory based on the pointer referenced by the variable and returns the memory to the heap. If the pointer is not pointing to a memory address in the heap, a run-time error occurs. If the pointer is pointing to a data structure that contains a long string, a variants, a dynamic array, or an interface, you must call Finalize before using Freemem.
Note: Using the new and Dispose procedures is stronger than using Getmem and Freemem. But when we use new and dispose, we don't need to show the call Finalize "

The translation is done. There must be inaccuracies in the place.
But I have a problem with memory allocation since new and dispose are easier to use than Getmem and Freemem, is it necessary to use Getmem, Freemem, and under what circumstances?

===================================================
Memory allocation since new and dispose are easier to use than Getmem and Freemem

"Easy-to-use" is usually only a relative concept, and here we discuss pointers to structs, in which case the new/dispose is often used. But they are limited, that is, the size of the space that the pointer points to must be able to be determined during compilation, before they know how much space they need to allocate. For pointers to structs, this value is the size of the struct, which of course is deterministic, so it can be used and facilitated.
However, there are some situations, such as you have only one Pointer type, this is an untyped pointer, you pass it to New, the compiler does not know what it points to, it does not know how much space it points to, it does not know how much space to allocate, it is not used, let alone ease of use.

Is it necessary to use Getmem, Freemem, and under what circumstances?

Of course, as mentioned earlier, new/dispose operations are more high-level, and we usually use them to manipulate pointers to structs, that is, the content that the pointer points to is the data structure known during compilation. And when we need to do some more low-level memory space, such as when you want to handle the string yourself, your pointer to the only you know or you have to understand the memory space, there is no explicit data structure during compilation and corresponding. At this time, we need to use the GETMEM/FREEMEM.
Therefore, the limitations of new/dispose are actually very large, or the scope of application is very small, and Getmem/freemem gives us full freedom, the trial scope is wider. Of course, the specific choice, but also depends on the actual situation.

http://blog.csdn.net/henreash/article/details/4235242

The difference between New and Getmem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.