Recent

Author Topic: What is the goal of assigned() ?  (Read 6830 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
What is the goal of assigned() ?
« on: April 06, 2021, 11:44:36 pm »
Hello.

Refering to this, I would like to know what is the utility of assigned() if the object must be assigned by something, ( nil for nothing )?

I understood that assigned() is true if the object <> nil.
But if the result is not assigned(), in fact he must be assigned as nil.

So what must be used to know if something was not assigned by something ( even not nil )?

Thanks.

Fre;D
« Last Edit: April 06, 2021, 11:53:00 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #1 on: April 07, 2021, 12:43:20 am »
its just a fancy way of using NIL as a compare value..

One is verbose and the other is binbose !  :D

And this one tribose ? :

Code: Pascal  [Select][+][-]
  1.  
  2.    procedure my_maybe_assigned_bitmap();
  3.     var   abitmap : TBGRABitmap;
  4.       begin
  5.         if assigned(abitmap) then abitmap.free;
  6.       end;

result:
 
Quote
   000000000418768 : EAccessViolation : Access violation

?
« Last Edit: April 07, 2021, 02:01:44 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: What is the goal of assigned() ?
« Reply #2 on: April 07, 2021, 12:44:39 am »
compatibility with Old pascal from the 90's where "assigned" checked if a FILE was or not ready to open or create (FCB file control block totally filled)
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: What is the goal of assigned() ?
« Reply #3 on: April 07, 2021, 12:49:44 am »
its just a fancy way of using NIL as a compare value..

One is verbose and the other is binbose !  :D

And this one tribose ? :

Code: Pascal  [Select][+][-]
  1.  
  2.   var
  3.     abitmap : TBGRABitmap;
  4. ...
  5.   procedure my_maybe_assigned_bitmap();
  6.       begin
  7.         if assigned(abitmap) then abitmap.free;
  8.       end;

result:
 
Quote
   000000000418768 : EAccessViolation : Access violation

?

NO NO AND NO

if assigned(reftomyclass) then FREEANDNIL(reftomyclass)

otherwise myclass.free do not set the reference to nil and if you pass the code again a fatal error is NORMAL
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: What is the goal of assigned() ?
« Reply #4 on: April 07, 2021, 12:53:57 am »
Code: Pascal  [Select][+][-]
  1. if assigned(P) then begin
  2.   P.free;
  3.  P:=nil;
  4. end;
  5.  

is equivalent to

if assigned(P) then FreeAndNil(P);
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #5 on: April 07, 2021, 01:02:46 am »
Code: Pascal  [Select][+][-]
  1. if assigned(P) then begin
  2.   P.free;
  3.  P:=nil;
  4. end;
  5.  

is equivalent to

if assigned(P) then FreeAndNil(P);

Many thanks, but I know that.

Huh, did you read my post?

With your code if P was not assigned by something included 'nil', your program will crash after if assigned(P)

And the question is: what to use if the object was assigned by nothing.
Using assigned() does not work for that.

Fre;D
« Last Edit: April 07, 2021, 01:11:56 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #6 on: April 07, 2021, 01:07:31 am »
I'll give up if you don't understand.. :o

Come on Jamie...

Maybe you did not understand what I asked.

I dont ask how to free something, the problem is not there, it was just used for the code.

I ask how to know if a object was not assigned nor initialized.

Fre;D

I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: What is the goal of assigned() ?
« Reply #7 on: April 07, 2021, 01:15:35 am »
I am a Windows person, sooo here I go:
Check that you have a valid range of addresses that you can read using IsBadReadPtr(pointer(abitmap), abitmap.InstanceSize)

We still don't know what's in these addresses. Repeat the same process based on the type you are checking.

For classes, the first address is a PPVMT, check the address it points at using IsBadReadPtr
Code: Pascal  [Select][+][-]
  1.  pp := PPVMT(abitmap);
and again for
Code: Pascal  [Select][+][-]
  1. . p := pp^;
and finally for the class name pointer. Then compare the class name.

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: What is the goal of assigned() ?
« Reply #8 on: April 07, 2021, 01:16:45 am »
hm just read the "this" link... remember Fred... variables on stack are random filled; variables on memory are zeroed at startup ;)

when compiler builds assembler code the begin of routines is standardized the stack pointer is decreased to make room for locals withouth initializing them to nil (zero) so they are random bytes (not nil until you set them to, manually)
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: What is the goal of assigned() ?
« Reply #9 on: April 07, 2021, 01:20:59 am »
addendum :::
variables on ram for basic types and classes are zeroed, OBJECT are random filled like stack variables (locals) and its up to you to assign them to nil or initialize them properly
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #10 on: April 07, 2021, 01:32:10 am »
@ Engkin and mercurhyo.

OK, many thanks and perfectly clear.

Clear that each object must be defined at declaration, otherwise only random decides.

So to resume, to do very good practice, when you create a new project with Lazarus, for example, the layout self-created is:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.  
  18.   public
  19.  
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1; // VERY BAD PRACTICE
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.FormCreate(Sender: TObject);
  32. begin
  33.  
  34. end;
  35.  
  36. end.

Should be for good Pascalian:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.  
  18.   public
  19.  
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1 = nil;  // GOOD BOY
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.FormCreate(Sender: TObject);
  32. begin
  33.  
  34. end;
  35.  
  36. end.

 ;)

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: What is the goal of assigned() ?
« Reply #11 on: April 07, 2021, 01:38:46 am »
form1: TForm;

is good practice LOL
1) it declares global variable form1 as type TForm
2) the declaration is global so the compiler takes care to make it NIL

if you do the same inside a procedure locals THEN you must set it yourself to nil because the variable does not reside in global mem but onto the stack

ex.
procedure blabla;
  var form1:TForm=nil;
...
the var inside a procedure/function is local to it and the compiler does not init locals to NIL it just makes room on stack and leaves random bytes inside
« Last Edit: April 07, 2021, 01:45:29 am by mercurhyo »
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: What is the goal of assigned() ?
« Reply #12 on: April 07, 2021, 01:42:21 am »
Global variables are given the value nil. You only need to take care of local variables.

I over complicated my previous post. you simple do the first check, and then make sure it is of the expected type. But I think you got the idea.
« Last Edit: April 07, 2021, 01:47:26 am by engkin »

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #13 on: April 07, 2021, 01:46:39 am »
form1: TForm

is good practice LOL
1) it declares global variable form1 as type TForm
2) the declaration is global so the compiler takes care to make it NIL

if you do the same inside a procedure locals THEN you must set it yourself to nil because the variable does not reside in global mem but onto the stack

ex.
procedure blabla;
  var form1:TForm=nil;
...
the var inside a procedure is local to it and the compiler does not init locals

Hum, and what about ( ouch, they will shout ) to propose to always assign to nil if nothing was assigned.

So, even in a local proc, if somebody does:

Code: Pascal  [Select][+][-]
  1. procedure blabla;
  2.   var form1:TForm;

the compiler will takes care to make form1 to NIL too?

Why this difference?



I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: What is the goal of assigned() ?
« Reply #14 on: April 07, 2021, 01:57:26 am »
Im sorry .. I've explained it carefully, if you can not understand between stack memory (locals) and heap memory (globals) its 2 different kinds of approach for all comilerS comprising C, Fortran, etc etc

its a standard from decades

local variables are ON STACK and never initialized , any compiler!
global variables are on HEAP and often initialized on modern compilers
by past nothing was initialized so you can feel lucky
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

 

TinyPortal © 2005-2018