Recent

Author Topic: Examples about pointers and RTTI ?  (Read 3360 times)

mosquito

  • Full Member
  • ***
  • Posts: 138
Examples about pointers and RTTI ?
« on: July 31, 2021, 04:33:55 pm »
I would like someone to put two examples of real life about this:

- Something that can not be done without using pointers. I do not mean something that would have more performance using them, but to something that literally can not do without using pointers.

- In the same way, something that can not be done without using RTTI.

Thank you and sorry for mixing two issues.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Examples about pointers and RTTI ?
« Reply #1 on: July 31, 2021, 05:47:56 pm »
I cannot answer your question about RTTI.  However, regarding pointers there are many things that cannot be done without pointers. I'll provide one of the typical examples.

Operating systems often provide one or more sort routines to sort any kind of data.  The only way they can sort any kind of data is for the program to provide the O/S sort routine with a compare function that enables it to determine if one element is less than, greater than or equal to another one.  The _only_ way to provide the sort routine with a function that determines the relationship between elements is by handing the sort routine a _pointer_ to the function that performs that task. 

There is no other way because the sort routine is already coded, it is not part of your program, you cannot add code to it to customize it.  The only thing you can do is code the compare function in your code and hand a pointer to it to the sort routine.

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

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Examples about pointers and RTTI ?
« Reply #2 on: July 31, 2021, 06:37:55 pm »
- In the same way, something that can not be done without using RTTI.

Serialization.

mosquito

  • Full Member
  • ***
  • Posts: 138
Re: Examples about pointers and RTTI ?
« Reply #3 on: July 31, 2021, 07:08:43 pm »
Thanks both  ;)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Examples about pointers and RTTI ?
« Reply #4 on: July 31, 2021, 08:33:24 pm »
I cannot comment about RTTI, I have not really used it.



If you want to understand pointer, maybe you can study the code of the GetRGB function in this thread:
https://forum.lazarus.freepascal.org/index.php/topic,37242.msg252828.html#msg252828

The information of pixels' colors are stored in the memory as an array. Some image types need 24 bit data for a single pixel but some others need 32 bit data for a single pixel. To simplified the code, I used absolute statement to set both Data24bit and Data32bit to occupy the same location of ScanData. (see lines #123 and #124)

The ScanData itself is a pointer. Because it is a pointer so I can point it to the memory location where the image data stores the pixel information. With the help of Data24bit and Data32bit, which are PRGBTriple and PRGBQuad, so can read the red, blue, green information easily.

So, what is a pointer typed variable? I would say it a variable that we can point it to any memory location, so we can easily read/write the data in the memory. For example you can use Inc function to make the pointer to point to the next memory location (see line #130, #138)

When 'playing' with pointers, you often need to use these symbols: @ and ^. You can check the documentation, what they mean and when to use them.



Another example is using pointer to create a keyboard buffer, which is very useful in game programming. In the thread in the link below, I showed how to manually using pointer and TList to create a keyboard buffer instead of using the ready-made TQueue.
https://forum.lazarus.freepascal.org/index.php/topic,38136.msg261965.html#msg261965



And if you want a more advanced example, yep I have it:
https://forum.lazarus.freepascal.org/index.php/topic,54548.msg406283.html#msg406283

To solve the case as requested by the OP in the thread, I need to store the information of the objects which must be unique. The only think in my head was the memory location of the object. So I used PtrInt(Button).ToString (see lines #69, #78, #92, #107). If you want to learn more about PtrInt, read here:
https://www.freepascal.org/docs-html/rtl/system/ptrint.html
« Last Edit: July 31, 2021, 08:59:49 pm by Handoko »

mosquito

  • Full Member
  • ***
  • Posts: 138
Re: Examples about pointers and RTTI ?
« Reply #5 on: July 31, 2021, 09:07:57 pm »
@Handoko I forgot to say that my interest was about declaring these pointers manually, since I know that some functions I use daily (the Math unit for example) use internally pointers and even assembly instructions. But I see that you caught the sense of my doubt. Preparing coffe, thank you very much.
« Last Edit: July 31, 2021, 09:30:39 pm by mosquito »

 

TinyPortal © 2005-2018