Recent

Author Topic: Interface typecast and reference counting [SKIPPED "advanced topic"]  (Read 2238 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
ما معنى الإسقاط typecast في الكود التالي وما هو الـ reference counting؟

google translate:

"What is the meaning of typecast in the following code and what is reference counting?"

https://www.freepascal.org/docs-html/current/ref/refse51.html

Code: Pascal  [Select][+][-]
  1. Var  
  2.   B : AClass;  
  3.  
  4. begin  
  5.   // ...  
  6.   AInterface(B.Intf).testproc;  
  7.   // ...  
  8. end;
  9.  
« Last Edit: May 29, 2021, 06:50:02 pm by pascal111 »
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Interface typecast and reference counting
« Reply #1 on: May 28, 2021, 07:44:17 pm »
To be able to better understand what typecasting is, you need to know what strong typing means. Not fully correct, but you can think typecasting as a command to convert the data to another type, for example from Integer to Real.
Read here if you want to learn more:
https://wiki.freepascal.org/Typecast

To be able to better understand what reference counting means, you need to know how the data being stored. Have you ever manually allocate some memory to a pointer and do some processing on it? If you have not, then it will be a bit difficult to explain reference counting to you. Reference counting is a technique to manage when the data in the memory can to be freed. If the counting = 0, it means the data can be freed because no one using it.
Read here if you want to learn more:
https://en.wikipedia.org/wiki/Reference_counting

For your information, AnsiString is reference counted but ShortString is not. You only will know why, if you understand how they store their data.
« Last Edit: May 28, 2021, 08:31:56 pm by Handoko »

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Interface typecast and reference counting
« Reply #2 on: May 29, 2021, 01:22:02 pm »
To be able to better understand what typecasting is, you need to know what strong typing means. Not fully correct, but you can think typecasting as a command to convert the data to another type, for example from Integer to Real.
Read here if you want to learn more:
https://wiki.freepascal.org/Typecast

أظنّني فهمت الفكرة نسبيّاً ،لكن ما يُحيرني هو لماذا في سطر الكود التالي تم استدعاء method من Interface وليس من صنف؟

google translate:

"I think I got the idea relatively, but what baffles me is why in the next line of code did a method call from the interface and not from a class?"

Code: Pascal  [Select][+][-]
  1.     Var  
  2.       B : AClass;  
  3.      
  4.     begin  
  5.       // ...  
  6.       AInterface(B.Intf).testproc;  
  7.       // ...  
  8.     end;
  9.      
  10.  


To be able to better understand what reference counting means, you need to know how the data being stored. Have you ever manually allocate some memory to a pointer and do some processing on it? If you have not, then it will be a bit difficult to explain reference counting to you. Reference counting is a technique to manage when the data in the memory can to be freed. If the counting = 0, it means the data can be freed because no one using it.
Read here if you want to learn more:
https://en.wikipedia.org/wiki/Reference_counting

For your information, AnsiString is reference counted but ShortString is not. You only will know why, if you understand how they store their data.

فلنفهمها الآن أنّها طريقةٌ لمعرفة عدم إستمرار الحاجة لمساحة ما في الذاكرة كانت مُخصصة لمتغير أو object مثلاً وسيتم تحريرها تلقائيّا.

google translate:

"Let us understand it now that it is a way to find out that some space in memory that was allocated to a variable or object, for example, will not continue to be needed, and it will be automatically edited "freed"."
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Interface typecast and reference counting
« Reply #3 on: May 29, 2021, 03:45:25 pm »
"... what baffles me is why in the next line of code did a method call from the interface and not from a class?"

Unfortunately I don't know much about interface so I can't explain it to you. I only know interface has something to do with multiple inheritance and it can help you write less code. So far I am happy with single inheritance concept. I have limited time to do programming and learning, if I have spare time I will use it to study new algorithms instead of interface.

"Let us understand it now that it is a way to find out that some space in memory that was allocated to a variable or object, for example, will not continue to be needed, and it will be automatically edited "freed"."

Something like that but not fully correct. Automatically freeing unused memory is the task for garbage collector. Reference counting only marks the variable as unused, it does not free the memory. Reference counting is one of the strategies used by garbage collector.
https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Strategies
« Last Edit: May 29, 2021, 03:47:10 pm by Handoko »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Interface typecast and reference counting
« Reply #4 on: May 29, 2021, 04:17:47 pm »
"Let us understand it now that it is a way to find out that some space in memory that was allocated to a variable or object, for example, will not continue to be needed, and it will be automatically edited "freed"."

Something like that but not fully correct. Automatically freeing unused memory is the task for garbage collector. Reference counting only marks the variable as unused, it does not free the memory.

I have to disagree here: with respect to managed types (strings, dynamic arrays), FPC adds code to free the memory once the reference count reaches zero. See e.g. the section on String Types in the Programmer's Guide:
Quote
The ansistring is a dynamically allocated string which has no length limitation (other than addressable memory). When the string is no longer being referenced (its reference count reaches zero), its memory is automatically freed. (emphasis mine)

It's true that maintaining the reference count and freeing the memory can be considered two distinct, independent operations but since they are simultaneous in FP (probably because the lack of a separate GC) they should, IMHO, thought of as one.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Interface typecast and reference counting
« Reply #5 on: May 29, 2021, 04:25:46 pm »
First, I didn't mention "only" used by garbage collector. Second, I was explaining reference counting in general not in Pascal. But yes, I understand choosing/using words can be tricky. In the case for AnsiString, to me it is a combination of reference counting technique + automatic memory freeing.
« Last Edit: May 29, 2021, 04:39:37 pm by Handoko »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Interface typecast and reference counting
« Reply #6 on: May 29, 2021, 04:42:19 pm »
I tend to agree with you in general terms w.r.t. reference counting.

It's just that the OP is, judging by his multiple questions on the theme, studying interfaces and related concepts as related to FP and, furtermore, has from mild to bewildering problems using English, so I think we should be as simple, concrete and to the point as we can.

It was in no way a reflection on you or your knowledge; excuse me if it seemed otherwise :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Interface typecast and reference counting
« Reply #7 on: May 29, 2021, 06:49:04 pm »
يُمكنني إعتبار هذا الموضوع من المواضيع المُتقدمة وبما أنّهُ يتضمّن كلاماً عن الذاكرة فلابدّ أنّه من المستوى الأدنى في البرمجة إذ أنّنا نقترب من دراسة كيفيّة عمل الأجزاء الداخليّة للمُترجم والآلة ولا يُمكننا التعبير عن ذلكـ بعربيّةٍ دقيقةٍ إلا إذا كنّا نفهم الموضوع ،فسأتركـ هذا الموضوع لمرحلةٍ أُخرى مُقبلة.

google translate:

"I can consider this topic as one of the advanced topics and since it includes speech about memory, it must be of the lowest level in programming as we are approaching the study of how the internal parts of the translator "compiler" and the machine work, and we cannot express this in accurate Arabic unless we understand the topic, so I will leave this topic for another next stage ."
La chose par la chose est rappelé.

 

TinyPortal © 2005-2018