Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers in Delphi

Tags:

delphi

Pointers can be still used in pascal and i think they may preserve it until delphi is alive.

Even though i have used pointer when i am learning pascal. I still can't understand the real use of pointers, I manage to do all my delphi programs without it.(by some other ways)

what is the real use of pointers. And I am asking for real world usage, and can we manage to do anything without pointers.

like image 971
VibeeshanRC Avatar asked Mar 05 '11 16:03

VibeeshanRC


People also ask

Does Delphi have pointers?

Another important pointer concept in Delphi is procedure and method pointers. Pointers that point to the address of a procedure or function are called procedural pointers. Method pointers are similar to procedure pointers. However, instead of pointing to standalone procedures, they must point to class methods.

What is pointers used for?

Pointers are used to store and manage the addresses of dynamically allocated blocks of memory. Such blocks are used to store data objects or arrays of objects. Most structured and object-oriented languages provide an area of memory, called the heap or free store, from which objects are dynamically allocated.

What are the three types of pointers?

There are majorly four types of pointers, they are: Null Pointer. Void Pointer. Wild Pointer.


2 Answers

You use pointers a lot more often than you think you do in Delphi. The compiler just hides it.

var
  SL: TStringList; 
...
  SL := TStringList.Create;
  // SL is now a pointer to an instance of the TStringList class.
  // You don't know it because the compiler handles dereferencing
  // it, so you don't have to use SL^ . You can just use the var.
  SL.Add('This is a string');

A string is also a pointer to a block of memory that stores the string. (It actually stores more than that, but...)

So are instances of records, PChars (which is a pointer to a character array, basically), and tons of other things you use every day. There are lots of pointers that aren't called Pointer. :)

like image 152
Ken White Avatar answered Nov 01 '22 12:11

Ken White


the pointers contain the address of a memory location, because that are present everywhere. each variable which you declare, even the code which you write can be accessed using a pointer, the pointers is one of the most essential elements in Win32 programming, you must read this great article from Rudy Velthuis Addressing pointers to understand the pointers usage.

like image 42
RRUZ Avatar answered Nov 01 '22 12:11

RRUZ