Recent

Author Topic: No pointers for Records?  (Read 896 times)

cousinp

  • New Member
  • *
  • Posts: 25
No pointers for Records?
« on: February 02, 2025, 07:45:17 pm »
Using the "Getting Started with Lazarus and Free Pascal" book.
When creating a Class -

Type
 TNames=class
end;

{Then} 
  var
    Names1 : TNames;
begin
  Names1 := TNames.Create;

I get the above construction and creation.
This is my question, the following is from the book.

Type
 TPerson = record
end;

var
    person1 : TPerson;
begin
    {Here is the question, there is no-}  person1 := TPerson. Create;

The program uses dot notation to access and specify the record fields.
Here is an example:   readln(person1. Name);

Is this an error in the book or is the creation of the pointer not
necessary with records.

Thanks in advance,
Paul
   

TRon

  • Hero Member
  • *****
  • Posts: 4133
Re: No pointers for Records?
« Reply #1 on: February 02, 2025, 08:03:27 pm »
To keep it simple for now: Only classes and object requires 'creation' so that memory for the object/class gets allocated. That is also why you need to (manually) free them once you are done with them.

You are allowed to create a record in memory if you want to though.

Perhaps a look at the wiki about record might help ? also this tutorial might perhaps be of help.
« Last Edit: February 02, 2025, 08:11:30 pm by TRon »
Today is tomorrow's yesterday.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5899
  • Compiler Developer
Re: No pointers for Records?
« Reply #2 on: February 02, 2025, 08:27:54 pm »
@cousinp: please use [code=pascal][/code]-tags when posting source code to avoid the forum potentially interpreting your code and also improving readability.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1477
    • Lebeau Software
Re: No pointers for Records?
« Reply #3 on: February 02, 2025, 09:57:11 pm »
Is this an error in the book


No.

is the creation of the pointer not necessary with records.

Correct. A record is a value type, which can be declared without allocating it dynamically. Unlike a class, which is a reference type that must be allocated dynamically.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

egsuh

  • Hero Member
  • *****
  • Posts: 1554
Re: No pointers for Records?
« Reply #4 on: February 03, 2025, 02:40:01 am »
When you define a record type variable, the memory is automatically allocated for it, but not with class. With class, only the space for address is allocated. For example,


Code: Pascal  [Select][+][-]
  1. type
  2.       TMyRec = record
  3.             X, Y : integer;
  4.       end;
  5.  
  6.       TMyClass = class
  7.             X, Y : integer;
  8.  
  9.             constructor Create; // you may omit this, unless you want to initialize, etc.
  10.       end;
  11.  
  12. var
  13.        MyRec : TMyRec;   // this allocates memory
  14.        MyClass : TMyClass; // this does not.
  15.  
  16. begin
  17.        MyClass := TMyClass.Create;
  18.  
  19.        MyClass.X := 10;   // this will raise exception if not created already;
  20.        MyRec.X := 15;  // here, you can directly access the variable's memory space
  21.  
  22.        MyClass.Free;
  23. end;


With record, there is pointer to record type. You can allocate memory to the pointer with New procedure, which will act similarly with class's constructor (create).

Code: Pascal  [Select][+][-]
  1. type
  2.       TMyRec = record
  3.             X, Y : integer;
  4.       end;
  5.       PMyRec = ^TMyRec;
  6.  
  7. var
  8.        MyRec : TMyRec;
  9.        MyRecPtr : PMyRec;
  10.  
  11. begin
  12.        New (MyRecPtr);      // this is similar to class's create
  13.        MyRecPtr^.X := 15;
  14.  
  15.        Release(MyRecPtr);   // and Free
  16. end;

The differences of class from record are encapsulation, inheritance, and polymorphism. But with advanced record, you can also add methods to the record, but records are not inherited (and polymorphism is meaningless as not inherited).

Object does not need constructor like record, but they are inheritable and polymorphic.

Code: Pascal  [Select][+][-]
  1. type
  2.       TMyObj = object
  3.             X, Y : integer;
  4.       end;
  5. var
  6.        MyObj : TMyObj;
  7.  
  8. begin
  9.        MyObj.X := 15;
  10. end;


Please study on object and class from other (better) sources.

440bx

  • Hero Member
  • *****
  • Posts: 5066
Re: No pointers for Records?
« Reply #5 on: February 03, 2025, 03:51:15 am »
The thing to keep in mind is:

Any type that is a pointer type does NOT allocate memory for whatever the pointer points to.  For instance, in the code:
Code: Pascal  [Select][+][-]
  1. type
  2.   PMyRecord = ^TMyRecord;
  3.   TMyRecord = record
  4.     SomeField    : integer;
  5.   end;
  6.  
  7. var
  8.   VRecordPointer   : PMyRecord;  // does NOT reserve memory for a TMyRecord
  9.   VRecordVariable  : TMyRecord;  // reserves memory for a TMyRecord
  10.  
The declaration VRecordPointer reserves memory to hold a pointer to a TMyRecord, it does NOT reserve memoy for a TMyRecord itself.

The declaration VRecordVariable reserves memory to hold a TMyRecord because the variable is a TMyRecord not just a pointer to it.

Now, in the code:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyClass = class
  3.     X, Y : integer;
  4.   end;
  5.  
  6. var
  7.   VMyClass : TMyClass;
  8.  
A class type is a pointer type, just like PMyRecord is a pointer type.  Unfortunately, this fact is "hidden" by the compiler, presumably for programmer's convenience. You can think of the word "class" as being an abbreviation for "pointer to record laid out as follows:"

Therefore the declaration "VMyClass : TMyClass";" is declaring a _pointer_ to the  structure/record.

You can think of it this way, in a single declaration, a class declares a pointer to the record and  its structure.  This is unlike with records which _require_ declaring the pointer and the structure separately.

Since a class is just a pointer to something, that's why it needs an additional step to reserve memory for it (usually by a Create method.)  The "var" declaration reserves memory for a pointer of that class, the "Create" call reserves memory to hold the structure the class points to.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

cousinp

  • New Member
  • *
  • Posts: 25
Re: No pointers for Records?
« Reply #6 on: February 05, 2025, 10:38:17 pm »
Thank you all very much.
Now I can turn to the next page.
Paul

 

TinyPortal © 2005-2018