[fpc-pascal] Function to create a record ?

Steve Litt slitt at troubleshooters.com
Fri Jun 2 02:36:37 CEST 2023


Hi all,

I'm trying to create a function (called newperson()) that creates a new
instance of a record. The following compiles with no errors or warnings
in FPC, but multiple calls to newperson() produce exactly the same
variable, so in the preceding changing person changes person2, and vice
versa:

===================================================
program test2;

const
	junkvar_size = 20000000;

type
	personrecord = record
	name: string;
	end;
var
	person: personrecord;
	person2: personrecord;
	junkvar: array[1..junkvar_size] of char;

function newperson(): personrecord;
var newp: personrecord;
begin
	newp.name := '';
	newperson := newp;
end;

function modperson(person: personrecord; name: string): personrecord;
begin
	person.name := name;
	modperson := person;
end;

procedure writeperson(person: personrecord);
begin
	writeln(person.name);
end;

begin
	person := newperson();
	fillchar(junkvar, junkvar_size, 'a');
	person2 := newperson();
	fillchar(junkvar, junkvar_size, 'b');
	person := modperson(person, 'Martin');
	person := modperson(person2, 'Maria');
	{writeln(junkvar);}
	writeperson(person);
	writeperson(person2);
end.
===================================================

What is the best way for me to construct newperson() so that every time
called it would return a new variable?

Thanks,

SteveT

Steve Litt 
Autumn 2022 featured book: Thriving in Tough Times
http://www.troubleshooters.com/bookstore/thrive.htm


More information about the fpc-pascal mailing list