开发者

How Delphi objects work in code [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have been looking at system.pas Delphi can call up to 65,000 memory blocks from windows When Delphi makes an object dose it call a memory block for its data And if so does the class some how load a register with a memory address for that memory block and an address for the methods too that is placed in another register. Does any one know anything about this?

J Lex Dean.


With GetMe开发者_如何学Gom you call from windows a memory block that windows allocates up to 65,000 per process inside the 4 gig space. Depending on the fag depends on if the block is moved when resized with the data relocated in the resize or fixed and other issues. Read about Windows or go to windows.pas and search on memory and call on your Delphi help Thier is a lot of funny things with system.pas like1/ _ObjectProcess as if to add confustion to delphi programmers. Why do they not just put code in TObject. 2/ And how does code measure deliration size of an object.


Have a look at this excellent explanation about what's going on when Delphi creates new Class instances: The Rise and Fall of TObject from Hallvard Vassbotn Although the original article appeared in 1998, most of it is still true for newer Delphi versions. Here is the original article from The Delphi Magazine:The Delphi Magazine, July 1998


Where does your "65000 memory blocks" statistic comes from?

When a class instance is Created, the following class method is called before executing the Create method of the class (from _ClassCreate global function, which ensures that the instance is created only once, for all Create nested calls):

class function TObject.NewInstance: TObject;

Which calls GetMem to get memory from the heap, and then the following method:

class function TObject.InitInstance(Instance: Pointer): TObject;

This InitInstance method will:

  1. Call FillChar() to put all the previously allocated memory to 0;
  2. Initialize the Interface Table of the object.

The methods (i.e. not the interfaces) are defined in the class type itself, not during class instance creation.

There is no "register" containing what you say.

You have access to the object memory address by its self variable, or by trans-typing its variable to a pointer:

var O: TObject;
begin
  O := TObject.Create;
  writeln('O memory address is ',pointer(O));
  O.Free;
end;

And before Delphi 2010 and its enhanced RTTI, you don't have access to all methods and fields of an object. Only published properties and methods are accessible to your code. But you must use RTTI. See TypInfo.pas unit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜