Forum > Beginners

Interface typecast and reference counting [SKIPPED "advanced topic"]

(1/2) > >>

pascal111:
ما معنى الإسقاط 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Var    B : AClass;   begin    // ...    AInterface(B.Intf).testproc;    // ...  end;  

Handoko:
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.

pascal111:

--- Quote from: Handoko 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

--- End quote ---

أظنّني فهمت الفكرة نسبيّاً ،لكن ما يُحيرني هو لماذا في سطر الكود التالي تم استدعاء 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    Var        B : AClass;           begin        // ...        AInterface(B.Intf).testproc;        // ...      end;      


--- Quote from: Handoko on May 28, 2021, 07:44:17 pm ---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.

--- End quote ---

فلنفهمها الآن أنّها طريقةٌ لمعرفة عدم إستمرار الحاجة لمساحة ما في الذاكرة كانت مُخصصة لمتغير أو 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"."

Handoko:

--- Quote from: pascal111 on May 29, 2021, 01:22:02 pm ---"... what baffles me is why in the next line of code did a method call from the interface and not from a class?"

--- End quote ---

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.


--- Quote from: pascal111 on May 29, 2021, 01:22:02 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"."

--- End quote ---

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

lucamar:

--- Quote from: Handoko on May 29, 2021, 03:45:25 pm ---
--- Quote from: pascal111 on May 29, 2021, 01:22:02 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"."

--- End quote ---

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.
--- End quote ---

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)
--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version