Recent

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

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: What is the goal of assigned() ?
« Reply #30 on: April 07, 2021, 06:17:05 pm »
I understand Fred Vs, the question is 100% valid and his points, but there is a solution, just in a procedure or method, just assign it to nil (FPC supports this).

Code: Pascal  [Select][+][-]
  1. procedure something();
  2. var
  3.   a: TMyObj = nil;
  4. begin
  5.   if not assigned(a) then
  6.    ShowMessage('It is nil!!');
  7. end;

Actually in Kotlin for example you must initialize with "null" if the variable is nullable or initialize the object with a class constructor. So is good practice also in modern languages.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #31 on: April 07, 2021, 06:18:15 pm »
@Fredvs

of course a compiler could zero reserved stack bytes BUT it will increase the final project size executable by 5%-15%. Good programmers prefer to manage locals on their own and spare exec time consum. and size. it is the same philosophy around the "I wana garbage collector" Holly Tradition says DEF NOOOO!!! you dont need a GC you need an exorcist hahaha

Ok, I forget garbage collector and auto-initialize local variable.

And, because initialization of variable is very important with fpc, might you remember this ?

 ;)

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

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #32 on: April 07, 2021, 06:22:52 pm »
I understand Fred Vs, the question is 100% valid and his points, but there is a solution, just in a procedure or method, just assign it to nil (FPC supports this).

Code: Pascal  [Select][+][-]
  1. procedure something();
  2. var
  3.   a: TMyObj = nil;
  4. begin
  5.   if not assigned(a) then
  6.    ShowMessage('It is nil!!');
  7. end;

Actually in Kotlin for example you must initialize with "null" if the variable is nullable or initialize the object with a class constructor. So is good practice also in modern languages.

Hello Lainz.

Yes, it is what was proposed in the first post (see the "this" link):
   
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2.     var
  3.     abitmap : TBGRABitmap = nil;
  4.     begin
  5.     if assigned(abitmap) then abitmap.free;
  6.     end;
« Last Edit: April 07, 2021, 06:32:08 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

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: What is the goal of assigned() ?
« Reply #33 on: April 07, 2021, 06:26:14 pm »
Thanks but I'm not winni  :D

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #34 on: April 07, 2021, 06:30:51 pm »
Thanks but I'm not winni  :D

Uy, lo siento Leandro, además de un exorcista, necesito unas gafas nuevas.

 :-[

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

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: What is the goal of assigned() ?
« Reply #35 on: April 07, 2021, 06:35:27 pm »
No pasa nada  :)

I have a big wish list for FPC, but I'm not compiler developer so I can do nothing as well. So I keep using it and adding stuff where I can, and where I can't well I use other languages so there is stuff to use everywhere =).

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: What is the goal of assigned() ?
« Reply #36 on: April 07, 2021, 07:14:52 pm »
I would like to know what is the utility of assigned() if the object must be assigned by something, ( nil for nothing )?

See Assigned or not Assigned, that is the question... for a detailed description from Allen Bauer of what Assigned() actually is, how it works, and why it exists (it was written for Delphi, but applies to FreePascal, too).

Check that you have a valid range of addresses that you can read using IsBadReadPtr(pointer(abitmap), abitmap.InstanceSize)

NO NO NO!

IsBadXxxPtr should really be called CrashProgramRandomly

There's no point improving the implementation of a bad idea

Is using IsBadReadPtr and IsBadWritePtr considered to be insecure?

Most efficient replacement for IsBadReadPtr?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: What is the goal of assigned() ?
« Reply #37 on: April 07, 2021, 07:34:40 pm »
I think this question comes as a different way of working with other languages.

Say in Javascript or Java or kotlin or anything else, we are used to check if something is not null.

What's the difference? With nil?

We don't have nullable types in FPC?

I know these objects in other languages just carry a boolean saying if it is null or not. Is that true?

We can benefit from that nullable approach in FPC?

balazsszekely

  • Guest
Re: What is the goal of assigned() ?
« Reply #38 on: April 07, 2021, 07:59:13 pm »
@lainz
Quote
We don't have nullable types in FPC?
Code: Pascal  [Select][+][-]
  1. uses Variants;
  2.  
  3. var
  4.   V: Variant;
  5. begin
  6.   V := null;
  7.   if VarIsNull(V) then
  8.     //...
  9. end;

:)

440bx

  • Hero Member
  • *****
  • Posts: 4029
Re: What is the goal of assigned() ?
« Reply #39 on: April 07, 2021, 08:13:35 pm »
NO NO NO!
I know discussions about the IsBadRead/WriteXXX functions are a can of worms but, I feel many of the concerns about them are overstated.

It is true that those functions can totally mess up the guard pages but, they wouldn't if there wasn't a bug (or at least a deficiency) in the code.

The bottom line is: if the program is bug-free those functions will not cause any harm.  OTH, if the program isn't bug free then the program's memory map (particularly its stack pages) will get messed up and this is very easy to notice using a virtual memory viewer (e.g, Process Hacker).  IOW, it's a very easy way of finding out if there is a bug somewhere in the program.  Additionally, they make it easy to find out where the bug is, put a breakpoint at lines that call them, execute the call and then watch what happened to the memory map.  If the map is ok, that call is ok,  if it is not ok then something is wrong.

Attached is a little program that shows what happens when IsBadReadPtr is _misused_.  It shows its own virtual memory map.  To see the effect of the misuse of the function, first locate a page with a "G" in the protect column (which means the memory block is used as a stack) then repeatedly select "update process map" from the "IsBadReadPtr" menu.  Watch how the size of the guarded block increases every time the map is updated (that shouldn't happen in a bug free program.)

PS: this is one of the examples I will eventually post the source of.  The attachment to this post only includes the executable.

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

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #40 on: April 07, 2021, 08:29:24 pm »
@lainz
Quote
We don't have nullable types in FPC?
Code: Pascal  [Select][+][-]
  1. uses Variants;
  2.  
  3. var
  4.   V: Variant;
  5. begin
  6.   V := null;
  7.   if VarIsNull(V) then
  8.     //...
  9. end;

:)

Hello GetMem.

OK, your solution does not produce a crash  with VarIsNull(V) if V was not initialized.

But the problem remains because if V was not initialized, the result of  VarIsNull(V)  will be false (because initialized with random):

Tested:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2.   var
  3.   V: Variant;
  4. begin
  5.   V := null;
  6.   if VarIsNull(V) then caption := 'V is null' else  caption := 'V is NOT null' ;
  7.  
  8. end;  

Gives as TForm1.caption :
Quote
'V is null'

But without initialization of V:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2.   var
  3.   V: Variant;
  4. begin
  5.   if VarIsNull(V) then caption := 'V is null' else  caption := 'V is NOT null' ;
  6. end;  

Gives as TForm1.caption :
Quote
'V is NOT null'

« Last Edit: April 07, 2021, 08:46:07 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

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1120
  • Professional amateur ;-P
Re: What is the goal of assigned() ?
« Reply #41 on: April 07, 2021, 09:20:09 pm »
Hey Fre;D,

In my view, Assigned and VarIsNull are opposites both semantically and logically.

One tests if something is assigned, hence not null. The other tests if something is null, hence not assigned.

I could be wrong, but are you not comparing apples and oranges?

If so, please correct me. I may be looking at this quite erroneously.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #42 on: April 07, 2021, 09:28:11 pm »
Hey Fre;D,

In my view, Assigned and VarIsNull are opposites both semantically and logically.

One tests if something is assigned, hence not null. The other tests if something is null, hence not assigned.

I could be wrong, but are you not comparing apples and oranges?

If so, please correct me. I may be looking at this quite erroneously.

Cheers,
Gus

Hello Gus.

Of course Assigned and VarIsNull are opposites both semantically and logically.

I was talking how both treat a local variable not initialized.
And both set as not nil/null a local variable not initialized (because, like explained in previous posts, a not initialized local variable is filled with random byte and not with nil/null).

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

balazsszekely

  • Guest
Re: What is the goal of assigned() ?
« Reply #43 on: April 07, 2021, 09:31:19 pm »
@Fred vS
Quote
But the problem remains because if V was not initialized, the result of  VarIsNull(V)  will be false (because initialized with random):
By default, a variant is unassigned or empty. Unassigned is the a lowest state in the variant hierarchy.  A null variant is already an assigned one, but has the value null. 

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1120
  • Professional amateur ;-P
Re: What is the goal of assigned() ?
« Reply #44 on: April 07, 2021, 09:35:43 pm »
Hey All,

After Remy pointed out this blog post: Assigned or not Assigned, that is the question..., my conclusions are:
  • Always initialize your variables with nil, well, where appropriate of course
  • Only reliably use Assigned for it's intended use: method addresses
  • The opposite of nil can be either assigned of pointing at random memory contents

This, nonetheless, leaves me with one burning question, or two:

How can I test if a variable has a valid instance of an object if the opposite of nil is not always assigned?
In my code, I can follow my rule of always initialising to nil, but how can I test that if I'm handed something from a black box of someone else's code?

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018