Throw please food for thought about pointers in DELPHI! No, I certainly read the mat part and worked with them (not fully understanding their nature), but why?

type TMyRec = class s:string; n:integer; end; var MyRec: TMyRec; PRec: ^TMyRec; begin //Но MyRec сейчас null на какую область памяти может начать ссылаться PRec? PRec:=@MyRec; PRec^.S:='Строка данных';//С помощью указателя производится изменение строкового поля записи PRec^.N:=256;//С помощью указателя производится изменение числового поля записи end; 

Everything is super, everything is great and works. But why, if the next record also works great?

 var MyRec: TMyRec; begin MyRec:=TMyRec.Create; MyRec.S:='Строка данных'; MyRec.N:=256; end; 

In general, I have a lot of questions on pointers that I can’t even articulate. I would be grateful for any information explaining why we need pointers in everyday life.

  • The main task of pointers is working with dynamically allocated memory, when at the compilation stage you do not know how much memory the program will need. - German Borisov
  • Because the delph compiler sees that PRec is a pointer and it allows you to use a dot. In c ++, this is not possible - there can be an overload and the behavior can be very different. - KoVadim
  • @HermanBorisov Actually, your comment has never clarified the situation! What does not suit such a record? MyRec:=TMyRec.Create; I also do not know how much memory will need nevertheless allocated it! - JVic
  • @Kromster Let there be a class, and what's the difference in the context of pointers? - JVic
  • one
    @Victor, look here for example delphisources.ru/pages/faq/base/dyn_list.html - Herman Borisov

2 answers 2

A pointer synonym is a link.
There is a memory, there is a link to the memory. When a TClass in a procedure, the pointer (link) to the object, not the object itself, will be transferred anyway.

When transferring a record without a pointer, the entire chunk of memory will be transferred to the procedure, not the reference, that is, the complete structure.

The pointer is inherited from Pascal from the time before the PLO, that is, when there were no classes at all. In Pascal, there are no classes of type TList and in order to do something similar, you need to fence your garden: TList = record S : string; Next : pointer; prev : pointer; end; PList = ^TList TList = record S : string; Next : pointer; prev : pointer; end; PList = ^TList TList = record S : string; Next : pointer; prev : pointer; end; PList = ^TList That is, pointers are needed mainly for the record and for compatibility with Pascal

  • Also, pointers are often needed for primitive types (Integer, etc.) - Kromster
  • The main thing here is not to understand by record only what you write in your delphi code. WinApi, and indeed any API in general, most often uses the structure of parameters of functions / methods. - teran
  • Yes, there is nothing to understand. There are just pieces of memory that need access. All the specifics of how memory is accessed and how data is located inside memory. - Albert Fomin

Dwell on

// But MyRec is now null

and

Everything is super, everything is great and works.

The local variable MyRec before assignment is not nil , but has an arbitrary value (garbage). You are just lucky, because in the following lines you write to an arbitrary area of ​​memory, and the consequences may be most unexpected.

Remember, in Sudden Access violation Delphi 10.1 Berlin, I changed your code

 try Props:=getProps; ... finally Props.Destroy; end; 

taking assignment for try

 Props := getProps; try ... finally Props.Free; end; 

The meaning of the line assigning the value of Props outside the try/finally block is that if there is an exception inside getProps , the code will not fall inside finally , and there will be no attempt to free the memory pointed to by the uninitialized local variable Props .

Another correct option:

 Props := nil; try Props := getProps; ... finally Props.Free; end;