Re: [fpc-pascal] Function to create a record ?

2023-06-04 Thread Steve Litt via fpc-pascal
Travis Siegel via fpc-pascal said on Sun, 4 Jun 2023 19:38:44 +

>
>On 6/4/2023 1:37 PM, Steve Litt via fpc-pascal wrote:
>> Henry Vermaak via fpc-pascal said on Fri, 2 Jun 2023 09:38:17 +0100
>>  
>>> On Fri, 2 Jun 2023 at 01:36, Steve Litt via fpc-pascal
>>>  wrote:  
  fillchar(junkvar, junkvar_size, 'b');
  person := modperson(person, 'Martin');
  person := modperson(person2, 'Maria');  
>>> Maybe a typo? (E.g. person2 := modperson(person2, 'Maria'))
>>>
>>> Henry  
>> Thanks Henry!
>>
>> You're right: Changing the second person := to person2 := did just
>> what I wanted it to do.
>>
>> Because returning a local array or string as a function return in C
>> leads to horrible intermittents due to the local going out of scope
>> and relinquishing the local's stack, which can then be overwritten,
>> I added a procedure, a function, and a main program statement which
>> each consumed two million bytes of local variables, and the person
>> and person2 didn't get overwritten. Very cool.
>>
>> When I increased the huge local vars to three million bytes, the
>> program segfaulted. I figure I just ran it out of stack, but until I
>> did, the huge local vars didn't trash the contents of person and
>> person2.
>>
>> Thanks for your help.
>>
>> SteveT


>Depending on the compiler *not* to overwrite local variables is most 
>certainly the wrong way to go with this.  You really should create 
>either a global record/object, or use functions to pass around the 
>information you need.  Expecting the operating system to keep local 
>variables after the area goes out of scope is just begging for errors 
>later on when the compiler/os treatment of said areas changes.

Thanks Travis,

This is the info I was asking for. Yeah, passing the local as a
function return worked, but because of a horrible experience doing that
in C, I was suspicious and leery of actually using that technique.

I'll just use new and dispose.

Thanks,

SteveT

Steve Litt 
Autumn 2022 featured book: Thriving in Tough Times
http://www.troubleshooters.com/bookstore/thrive.htm
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Choice of exceptions to use?

2023-06-04 Thread Bart via fpc-pascal
On Sun, Jun 4, 2023 at 11:58 AM Graeme Geldenhuys via fpc-pascal
 wrote:

> Initially I was leaning towards the Range exception, but now I'm
> thinking that maybe the Argument exception is more appropriate
> (based on the descriptions seen in the linked docs).

I would go for range exception, since the offending coordinates are
out of range.
Invalid Argument for me would apply to situations like a wrong type
for a format specifier.


-- 
Bart
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Error: Argument cannot be assigned to

2023-06-04 Thread Juha Manninen via fpc-pascal
OK, I understand mostly. Thanks.

Juha
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Function to create a record ?

2023-06-04 Thread Travis Siegel via fpc-pascal
Depending on the compiler *not* to overwrite local variables is most 
certainly the wrong way to go with this.  You really should create 
either a global record/object, or use functions to pass around the 
information you need.  Expecting the operating system to keep local 
variables after the area goes out of scope is just begging for errors 
later on when the compiler/os treatment of said areas changes.



On 6/4/2023 1:37 PM, Steve Litt via fpc-pascal wrote:

Henry Vermaak via fpc-pascal said on Fri, 2 Jun 2023 09:38:17 +0100


On Fri, 2 Jun 2023 at 01:36, Steve Litt via fpc-pascal
 wrote:

 fillchar(junkvar, junkvar_size, 'b');
 person := modperson(person, 'Martin');
 person := modperson(person2, 'Maria');

Maybe a typo? (E.g. person2 := modperson(person2, 'Maria'))

Henry

Thanks Henry!

You're right: Changing the second person := to person2 := did just what
I wanted it to do.

Because returning a local array or string as a function return in C
leads to horrible intermittents due to the local going out of scope and
relinquishing the local's stack, which can then be overwritten, I added
a procedure, a function, and a main program statement which each
consumed two million bytes of local variables, and the person and
person2 didn't get overwritten. Very cool.

When I increased the huge local vars to three million bytes, the
program segfaulted. I figure I just ran it out of stack, but until I
did, the huge local vars didn't trash the contents of person and
person2.

Thanks for your help.

SteveT

Steve Litt
Autumn 2022 featured book: Thriving in Tough Times
http://www.troubleshooters.com/bookstore/thrive.htm
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Error: Argument cannot be assigned to

2023-06-04 Thread Martin Frb via fpc-pascal

On 04/06/2023 17:49, Martin via fpc-pascal wrote:

On 04/06/2023 15:04, Juha Manninen via fpc-pascal wrote:

Why the following code fails to compile?
MyObj.RecInstance.ii := 123;
Technically you can modify the members of the temp record (just to 
have the work thrown away afterwards).


So I guess the compiler forbids it, because the only case it would 
ever be done is: by mistake.


Btw, this works (or actually not, it works wrong)

  with  MyObj.RecInstance do {begin} ii := 123; {end;}

Now you could do some work with the temp result, and have it trashed at 
the end of the "with" block.


However, fpc incorrectly changes the value in the original record.
Yet, don't use it. It will (likely) change, and stop working. (or work 
correctly, changing the temp value only).

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Error: Argument cannot be assigned to

2023-06-04 Thread Martin Frb via fpc-pascal

On 04/06/2023 15:04, Juha Manninen via fpc-pascal wrote:

Why the following code fails to compile?

  TMyRec = record

...

property
    RecInstance: TMyRec read fRecInstance;// write fRecInstance;

...

MyObj.RecInstance.ii := 123;
Access through property seems to be the problem. Accessing 
fRecInstance directly works.




A property enforces that any code you write will work even if you change 
the property to a getter/setter.

Or in other words, it will fail any code that would not work.

If this was a getter, it would be
  function GetRec: TMyRec;

And that returns a *copy* in a temporary location of the record.
Technically you can modify the members of the temp record (just to have 
the work thrown away afterwards).


So I guess the compiler forbids it, because the only case it would ever 
be done is: by mistake.


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Error: Argument cannot be assigned to

2023-06-04 Thread Martin via fpc-pascal

On 04/06/2023 15:04, Juha Manninen via fpc-pascal wrote:

Why the following code fails to compile?

  TMyRec = record

...

property
    RecInstance: TMyRec read fRecInstance;// write fRecInstance;

...

MyObj.RecInstance.ii := 123;
Access through property seems to be the problem. Accessing 
fRecInstance directly works.




A property enforces that any code you write will work even if you change 
the property to a getter/setter.

Or in other words, it will fail any code that would not work.

If this was a getter, it would be
  function GetRec: TMyRec;

And that returns a *copy* in a temporary location of the record.
Technically you can modify the members of the temp record (just to have 
the work thrown away afterwards).


So I guess the compiler forbids it, because the only case it would ever 
be done is: by mistake.



___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Error: Argument cannot be assigned to

2023-06-04 Thread Juha Manninen via fpc-pascal
On Sunday, June 4, 2023, Mattias Gaertner via fpc-pascal <
fpc-pascal@lists.freepascal.org> wrote:

>
> Correct. Property RecInstance is read only.
>

No, I can define it as :
property RecInstance: TMyRec read fRecInstance write fRecInstance;
and still get the same error.

Juha
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Error: Argument cannot be assigned to

2023-06-04 Thread Mattias Gaertner via fpc-pascal
On Sun, 4 Jun 2023 16:04:48 +0300
Juha Manninen via fpc-pascal  wrote:

> Why the following code fails to compile?
> 
> type
>   TMyRec = record
> ss: String;
> ii: Integer;
>   end;
>   TMyClass = class
>   private
> fName: String;
> fRecInstance: TMyRec;
>   property
> RecInstance: TMyRec read fRecInstance;// write fRecInstance;
>   end;
> var
>   MyObj : TMyClass;
> begin
>   MyObj := TMyClass.Create;
>   MyObj.RecInstance.ii := 123;
>   MyObj.Free;
> end.
> 
> Access through property seems to be the problem. Accessing
> fRecInstance directly works.

Correct. Property RecInstance is read only.

Mattias
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Function to create a record ?

2023-06-04 Thread Steve Litt via fpc-pascal
Ralf Quint via fpc-pascal said on Thu, 1 Jun 2023 17:43:58 -0700

>On 6/1/2023 5:36 PM, Steve Litt via fpc-pascal wrote:

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

>W.T.F.?
>
>Sorry, but for how long are you programming, in general, and for how 
>long in Pascal?
>
>Seriously, you should take some very basic Pascal tutorials, work 
>through them, and in particular, pay close attention to the "scope" of 
>variables as well as parameter passing. You seem to be completely 
>oblivious to those very basics here... :(
>
>Ralf

Feeling better after getting that off your chest Ralf?

My  post reveals the outcome of my investigation.

SteveT

Steve Litt 
Autumn 2022 featured book: Thriving in Tough Times
http://www.troubleshooters.com/bookstore/thrive.htm
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Function to create a record ?

2023-06-04 Thread Steve Litt via fpc-pascal
Henry Vermaak via fpc-pascal said on Fri, 2 Jun 2023 09:38:17 +0100

>On Fri, 2 Jun 2023 at 01:36, Steve Litt via fpc-pascal
> wrote:
>> fillchar(junkvar, junkvar_size, 'b');
>> person := modperson(person, 'Martin');
>> person := modperson(person2, 'Maria');  
>
>Maybe a typo? (E.g. person2 := modperson(person2, 'Maria'))
>
>Henry

Thanks Henry!

You're right: Changing the second person := to person2 := did just what
I wanted it to do.

Because returning a local array or string as a function return in C
leads to horrible intermittents due to the local going out of scope and
relinquishing the local's stack, which can then be overwritten, I added
a procedure, a function, and a main program statement which each
consumed two million bytes of local variables, and the person and
person2 didn't get overwritten. Very cool.

When I increased the huge local vars to three million bytes, the
program segfaulted. I figure I just ran it out of stack, but until I
did, the huge local vars didn't trash the contents of person and
person2.

Thanks for your help.

SteveT

Steve Litt 
Autumn 2022 featured book: Thriving in Tough Times
http://www.troubleshooters.com/bookstore/thrive.htm
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Error: Argument cannot be assigned to

2023-06-04 Thread Juha Manninen via fpc-pascal
Formatting was a little weird. This is what I meant, but the error remains :

type
  TMyRec = record
ss: String;
ii: Integer;
  end;
  TMyClass = class
  private
fName: String;
fRecInstance: TMyRec;
  public
property RecInstance: TMyRec read fRecInstance;// write fRecInstance;
  end;
var
  MyObj : TMyClass;
begin
  MyObj := TMyClass.Create;
  MyObj.RecInstance.ii := 123;
  MyObj.Free;
end.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Error: Argument cannot be assigned to

2023-06-04 Thread Juha Manninen via fpc-pascal
Compiler version is FPC 3.2.2 on Linux.

Juha
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Error: Argument cannot be assigned to

2023-06-04 Thread Juha Manninen via fpc-pascal
Why the following code fails to compile?

type
  TMyRec = record
ss: String;
ii: Integer;
  end;
  TMyClass = class
  private
fName: String;
fRecInstance: TMyRec;
  property
RecInstance: TMyRec read fRecInstance;// write fRecInstance;
  end;
var
  MyObj : TMyClass;
begin
  MyObj := TMyClass.Create;
  MyObj.RecInstance.ii := 123;
  MyObj.Free;
end.

Access through property seems to be the problem. Accessing fRecInstance
directly works.

Juha
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Choice of exceptions to use?

2023-06-04 Thread Graeme Geldenhuys via fpc-pascal
Hi,

I'm working on code where the function take a (x, y) set of coordinates.
If the coordinates are out of range/bounds, I want to raise an exception
with a message explaining the reason and limits. I definitely don't want
to "silently do nothing".

Looking at these set of built-in exceptions:
  https://www.freepascal.org/docs-html/rtl/sysutils/index-4.html


I'm in two-minds as to which exception to raise. Seems my choices are:

* EArgumentException - an invalid argument was passed to the method
* ERangeException - I could interpret this as the values passed in,
were not in the allowed range.

Initially I was leaning towards the Range exception, but now I'm
thinking that maybe the Argument exception is more appropriate
(based on the descriptions seen in the linked docs).

What's your thoughts?

Regards,
  Graeme
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal