Recent

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

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: What is the goal of assigned() ?
« Reply #15 on: April 07, 2021, 01:58:26 am »
Hum, and what about ( ouch, they will shout ) to propose to always assign to nil if nothing was assigned.

Maybe the other way around: why automatically initialize anything? Let that be programmers responsibility (of course, that would probably break some existing code  :)).

If I declare some local variable it would be waste of CPU cycles if variable is initialized every time and I create object by myself:
Code: Pascal  [Select][+][-]
  1. procedure TestProc;
  2. var sl:TStringList;
  3. begin
  4.   sl:=TStringList.Create;
  5.   try
  6.     ...
  7.   finally
  8.     sl.Free;
  9.   end;
  10. end;
« Last Edit: April 07, 2021, 02:00:07 am by dseligo »

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #16 on: April 07, 2021, 02:10:25 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

OK, I feel lucky.

But for the future I would be lucky too if HEAP and STACK will always initialize his vars on a modern compiler like FPC.

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 #17 on: April 07, 2021, 02:59:41 am »
@ Mercurhyo:

The last one, to be totally clear :

Quote
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)

Then locals are not initialized to make room and random bytes are used instead.
So the stupid question, how less room random bytes use vs nil?

Thanks.

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

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1122
  • Professional amateur ;-P
Re: What is the goal of assigned() ?
« Reply #18 on: April 07, 2021, 03:09:30 am »
Hey all,

After a bit of a back and forth with the whole AObject.Free and the nil thing, which wasn't the OP's intention to deviate that way, I still can't understand what is the function Assigned() good for.

I think that's what Fre;D really wants to know.

If we take the word assigned at face value, intuitively it should return True when someone has called AObject:= TAObject.Create and False if set explicitly to nil or if it's un-initialised.

That's what I take at face value.

It's all good to mention how the stack behaves and all, quite informative, but it's doesn't answer the OP's question: When does it make sense to use it?

For me a good answer would be a bullet list with simple situations...

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 #19 on: April 07, 2021, 03:26:27 am »
Quote
For me a good answer would be a bullet list with simple situations...

What I did understood is that assigned() should be used safely only for global variable.
For local variable, assigned() should be used only if the variable was initialized by nil or something else before assigned() is called.

Fre;D
« Last Edit: April 07, 2021, 03:29:35 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

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: What is the goal of assigned() ?
« Reply #20 on: April 07, 2021, 03:34:22 am »
Don't forget the compiler will let you know if a local variable was not initialized. Paying attention to these messages is probably a safer way.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #21 on: April 07, 2021, 03:42:24 am »
Don't forget the compiler will let you know if a local variable was not initialized. Paying attention to these messages is probably a safer way.

Hum, so the compiler knows it...

Nice, and what is the parameter to force him to initialize automatically it for the user?

...

OK, ok, ok I go to bed and close the door.

Many thanks Engkin, lot of things clear now.

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

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: What is the goal of assigned() ?
« Reply #22 on: April 07, 2021, 08:21:09 am »
After a bit of a back and forth with the whole AObject.Free and the nil thing, which wasn't the OP's intention to deviate that way, I still can't understand what is the function Assigned() good for.

From https://www.freepascal.org/docs-html/rtl/system/assigned.html: 'Assigned returns True if P is non-nil and returns False of P is nil. The main use of Assigned is that Procedural variables, method variables and class-type variables also can be passed to Assigned.'

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: What is the goal of assigned() ?
« Reply #23 on: April 07, 2021, 08:28:22 am »
Then locals are not initialized to make room and random bytes are used instead.
So the stupid question, how less room random bytes use vs nil?
They don't use less room, they use less code because there is no code for initialization. And what is the point of (local) variable if you don't initialize it? And you don't always need variable with double type to be 0.0, or integer to be 0, or string to be ''. You found example where you need nil value for your object, but you usually use locally defined objects, and you will create them.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: What is the goal of assigned() ?
« Reply #24 on: April 07, 2021, 09:25:31 am »
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've read the first few messages of the thread but thought I could put it more succinctly.

It's so that you can check whether a method is assigned, where that method is a parameterless function that can legitimately return nil.

The necessity is due to parameterless functions in Pascal not requiring empty parentheses, hence there being no reliable way to distinguish between the address of (or pointer to) a function and the result of its invocation.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: What is the goal of assigned() ?
« Reply #25 on: April 07, 2021, 09:56:24 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)

This can lead to false positives, because IsBadReadPtr determines its result based upon the mapped virtual pages. However if a type is smaller than a page then it might be freed but the heap manager will still keep a hold of the page if something else is still allocated there. And even if it's all free the heap manager might hold onto the page for performance reasons. Take a look at this:

Code: Pascal  [Select][+][-]
  1. program tbadptr;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Windows;
  7.  
  8. var
  9.   p1, p2: PLongInt;
  10. begin
  11.   New(p1);
  12.   New(p2);
  13.   Writeln(IsBadReadPtr(p1, SizeOf(LongInt)));
  14.   Writeln(IsBadReadPtr(p2, SizeOf(LongInt)));
  15.   Dispose(p1);
  16.   Writeln(IsBadReadPtr(p1, SizeOf(LongInt)));
  17.   Writeln(IsBadReadPtr(p2, SizeOf(LongInt)));
  18.   Dispose(p2);
  19.   Writeln(IsBadReadPtr(p1, SizeOf(LongInt)));
  20.   Writeln(IsBadReadPtr(p2, SizeOf(LongInt)));
  21. end.

This will write FALSE six times.

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?

The difference is that global variables are initialized by the operating system when loading the binary (at least in modern OSes), thus those are set to 0, Nil, etc. for free. For local variables that needs to be done actively thus it might result in the addition of instructions that are simply not necessary because the variable is assigned a value later on.

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 )?

It's very expensive and potentially non-computable to check for "Is not valid", because this would mean to walk the complete heap and other such things and even then one might not catch everything. Thus things like that are simply not done and Assigned only checks for Nil or not.

kupferstecher

  • Hero Member
  • *****
  • Posts: 583
Re: What is the goal of assigned() ?
« Reply #26 on: April 07, 2021, 12:06:19 pm »
Then locals are not initialized to make room and random bytes are used instead.
No, its not filled with random bytes, but memory is assigned (for local variables in the stack) and that normally contains 'old' data. E.g. the local variable of a function that was called before or any other stuff that was pushed onto the stack before.

The compiler could insert extra code to zero that memory, but it rather keeps the execution fast. And it warns you in case you try to use an uninitialized variable. When you create a class all variables of the instance are zeroed/nil'ed. Also global variables are initialised, so in 'normal coding' practice this leaves only local variables that are not initialised (And arrays? Can someone confirm?). Working with pointers on memory blocks is another thing, I don't categorize it as 'normal coding' practice.

Here the manual section about initialized variables.
https://freepascal.org/docs-html/ref/refse24.html

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: What is the goal of assigned() ?
« Reply #27 on: April 07, 2021, 05:34:43 pm »
Then locals are not initialized to make room and random bytes are used instead.
No, its not filled with random bytes, but memory is assigned (for local variables in the stack) and that normally contains 'old' data. E.g. the local variable of a function that was called before or any other stuff that was pushed onto the stack before.

The compiler could insert extra code to zero that memory, but it rather keeps the execution fast. And it warns you in case you try to use an uninitialized variable. When you create a class all variables of the instance are zeroed/nil'ed. Also global variables are initialised, so in 'normal coding' practice this leaves only local variables that are not initialised (And arrays? Can someone confirm?). Working with pointers on memory blocks is another thing, I don't categorize it as 'normal coding' practice.

Here the manual section about initialized variables.
https://freepascal.org/docs-html/ref/refse24.html

yep you're right but that is what I call "random bytes" bytes that make random fatal errors (by my convenience) without the need to write a whole piece of cake of explainaitions on stack management to people who are poor at assembly level  >:D stack management is another subject i think... a deeper one
« Last Edit: April 07, 2021, 05:36:34 pm 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

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: What is the goal of assigned() ?
« Reply #28 on: April 07, 2021, 05:39:10 pm »
No, its not filled with random bytes, but memory is assigned (for local variables in the stack) and that normally contains 'old' data. E.g. the local variable of a function that was called before or any other stuff that was pushed onto the stack before.

"Filled with random bytes" is entirely adequate IMO, the other case being "explicitly randomised".

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

mercurhyo

  • Full Member
  • ***
  • Posts: 242
Re: What is the goal of assigned() ?
« Reply #29 on: April 07, 2021, 05:43:55 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
« Last Edit: April 07, 2021, 05:54:40 pm 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

 

TinyPortal © 2005-2018