[fpc-pascal] Function to create a record ?

Steve Litt slitt at troubleshooters.com
Tue Jun 6 03:34:26 CEST 2023


Jean SUZINEAU via fpc-pascal said on Mon, 5 Jun 2023 11:56:59 +0200

>Hello, I have a hard time trying to understand what you want to test.
>
>In your functions, you are using a record, not a class or a pointer to 
>record or an object.
>
>The personrecord just behave in your parameters and return value as an 
>Integer or a String, and your fillchar doesn't write outside of the 
>memory allocated for you junkvar, so I don't see what can go wrong.
>
>If you trace the address of person.name like this:
>
>//begin code
>begin
>	person := newperson();
>         writeln('1, person.name address: ',
> IntToHex(Int64(Pointer(@(person.name)))));
>	fillchar(junkvar, junkvar_size, 'a');
>	person2 := newperson();
>	fillchar(junkvar, junkvar_size, 'b');
>	person := modperson(person, 'Martin');
>	person := modperson(person2, 'Maria');
>         writeln('2, person.name address: ',
> IntToHex(Int64(Pointer(@(person.name)))));
>	{writeln(junkvar);}
>	writeperson(person);
>	writeperson(person2);
>end.
>//end code
>
>You'll see that the address doesn't change, for example:
>
>1, person.name address: 000000000050C7F0
>2, person.name address: 000000000050C7F0
>
>
>But if you declare your personrecord as a class, the things doesn't 
>behave the same way:
>
>//begin code
>const
>	junkvar_size = 20000000;
>
>type
>	personclass = class
>	name: string;
>	end;
>var
>	person: personclass;
>	person2: personclass;
>	junkvar: array[1..junkvar_size] of char;
>
>function newperson(): personclass;
>var newp: personclass;
>begin
>      newp:= personclass.Create;
>	newp.name := '';
>	newperson := newp;
>end;
>
>function modperson(person: personclass; name: string): personclass;
>begin
>	person.name := name;
>	modperson := person;
>end;
>
>procedure writeperson(person: personclass);
>begin
>	writeln(person.name);
>end;
>
>begin
>	person := newperson();
>         writeln('1, person.name address: ',
> IntToHex(Int64(Pointer(@(person.name)))));
>	fillchar(junkvar, junkvar_size, 'a');
>	person2 := newperson();
>	fillchar(junkvar, junkvar_size, 'b');
>	person := modperson(person, 'Martin');
>	person := modperson(person2, 'Maria');
>         writeln('2, person.name address: ',
> IntToHex(Int64(Pointer(@(person.name)))));
>	{writeln(junkvar);}
>	writeperson(person);
>	writeperson(person2);
>end.
>//end code
>
>
>You'll get a different address, something like:
>
>
>1, person.name address: 00007FFFF7F910E8
>2, person.name address: 00007FFFF7F91108

Thanks Jean,

The problem I was trying to fix was that I accidentally said:

person := modperson(person2, 'Maria');

instead of 

person2 := modperson(person2, 'Maria');
^^^^^^^  Note the 2

But wait, there's more...

As Travis Siegel pointed out, my method relied on the compiler not to
overwrite RAM of a variable that had gone out of scope, and that's the
highway to heartache. So I was first going to implement a non-OOP
solution using New and Dispose, and then implement an OOP solution. Now
that you provided the OOP solution, I'll make sure I can get your
solution to work, and then go back to the non-OOP solution.

So **THANK YOU** ! ! !

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