Vibeeshan Mahadeva
Vibeeshan Mahadeva

Reputation: 7248

Pointers in 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.

Upvotes: 10

Views: 1907

Answers (8)

fmotis
fmotis

Reputation: 286

I thought I could add my salt to the soup, but the answers above say it mostly all. There's one more thing though. I remember being scared when I saw all those referencing and dereferencing operators (in different languages) and all the magic that was done with pointers. I didn't dare to look the devil in the eye for YEARS. I preferred to ineffectively copy data instead of going down the pointer path.

Today though, I do love pointers. Working with pointers makes you aware of what happens under the hood. And it makes you aware of the memory you are playing with (responsibly). Practically it allows you to code more effitiently and consciously! Last but not least it turns out to be quite fun to play with such simple but powerful toys.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613441

Let's start with a definition, taken from Wikipedia:

A pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.

All computers address memory and to do so the machine language that they execute must do so using pointers.

However, high level languages do not need to include pointers explicitly. Some examples of those that do not are LISP, Java, C#, Perl, Python, but there are many more.

I'm interpreting your question to be why languages support explicit pointer use, since all languages use pointers implicitly.

Delphi descends from Pascal which is a rather primitive language when viewed from the 21st century. Pascal pointers are the only way to use references. In modern Pascal derivatives, e.g. Delphi, have many types of data that are reference based, but implicitly so. For example I'm thinking of strings, object instances, interfaces and so on.

It is perfectly possible to write any program in Delphi without resorting to explicit pointers. The modern trend away from explicit pointers is down to the observation that explicit pointer code is more prone to errors than the alternatives.

I don't think there's any real reason to carry on using explicit pointer code in Delphi. Perhaps very time critical algorithms may push you that way, but I'm really struggling to think of anything that is significantly better implemented with pointers than with the alternatives.

Personally I avoid using explicit pointers wherever feasible. It generally makes code easier to write, verify and maintain, in my experience.

Upvotes: 4

user160694
user160694

Reputation:

Pointers (memory addresses) are a basic CPU type to access memory. Whatever language you use to write an application, when it comes to the CPU it has to use "pointers" to work. Thereby they are a low-level type which allows a huge degree of versatility, at the price of a somewhat more complex management and the risk of accessing or writing the wrong memory if not used correctly. You could let a language translate your data to pointers and memory buffers, or use pointers directly if the language allows for it. Being able to do it allows for direct and optimized memory access and management.

Upvotes: 1

Cosmin Prund
Cosmin Prund

Reputation: 25678

To understand what pointers might be used for in modern day Delphi, one needs to understand how pointers have been historically used and how Delphi uses pointers behind the scenes.

Pointers to code

One can have a pointer to a piece of code. This can be used for the intuitive thing (parameterizing some algorithm with the functions it needs to call; example: TObjectList.Sort takes a function pointer as a parameter). Modern Delphi uses pointers-to-code to implement the following (without going into details):

  • Virtual Method Tables; We can't have OOP without VMT's
  • Interfaces
  • Events and anonymous methods.

Those are all very powerful methods of avoiding raw pointers to code, indeed there's very little need for raw code pointers today.

Pointers to data

Everybody learned pointers using the Linked Lists. Pointers are essential in the implementation most non-trivial data structures; In fact it's hard to name a useful data structure that's not implemented using pointers.

Delphi gives lots of grate abstractions for data pointers, so we can work without ever touching an pointer. We have Objects (class instances) and good implementations for most data structures (string, TObjectList, dynamic arrays).

When do we use Pointers?

We essentially use pointers to implement more of the grate stuff that Delphi provides for us. I can give examples of what I've used pointers for, but I find it more interesting to give examples of what others have used pointers for:

  • TVirtualTree: Makes good use of pointers to data.
  • Pascal Script for Delphi: Makes extensive use of raw pointers to code!

Upvotes: 5

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109003

I use a lot of custom data structures in my programs (for instance, in my own scripting language /interpreter/, where I store the structures, or "records", used in this language - these can contain other records and the nesting is unrestricted). I do this at the very lowest level, namely, I allocate a number of bytes on the heap, and then I read, parse, and write to these completely manually. To this end, I need pointers to point to the bytes in the allocated blocks. Typically, the blocks consists of "records" that reference each other in some ways. In addition, many of these structures can be written to file (that is, I have also designed my own binary file formats), simply by copying from the heap (in RAM) to the disk byte-by-byte.

Upvotes: 1

RRUZ
RRUZ

Reputation: 136431

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.

Upvotes: 12

Ken White
Ken White

Reputation: 125749

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

Upvotes: 13

Loren Pechtel
Loren Pechtel

Reputation: 9093

1) Classes are pointers. Strings are pointers. The compiler pretty much hides this from you but you do need to understand this when debugging.

2) Any sort of tree structure needs pointers.

Upvotes: 2

Related Questions