Hi everyone,
Here are some comments on Pointers when used for data to expand on some of
the benefits already listed.
I've used Pointers for Linked Lists for years, ever since I used to program
in Turbo Pascal. You set up a structure or record for a data item (each of
which may be small, but also can contain quite a lot of data with many
fields) and link them together with pointers. (You have a first in chain
pointer, then each record points forward to the next record, and also
backwards as needed.) It's all described in the text books I used to read,
and presumably still is.
The benefits are:
- You can insert a new record by altering a couple of pointers, no need to
move lots of data around.
- You can sort the list just by swapping pointers, again, no need to move a
lot of data around.
- You can pass data records to functions and methods easily just by passing
a pointer (ie by reference) There's no data to move around, and the called
function can work directly with the data.
- You can delete an item without having to move any data, just change a
couple of pointers and free the storage.
- The maximum chunk of memory you need to allocate at any one time is just
the size of one record; important if you are running short of memory and/or
this is getting fragmented.
That's just for starters.
I was always surprised how fast the programs that used linked lists to hold
data were in the good old days of 286 and 386 machines and Turbo Pascal. I
couldn't have used any sort of database and obtained the performance I
needed.
Nowadays I expect that the performance is still excellent compared with
other methods; having said that, this may no longer matter. Now that now we
have dynamic arrays I often use these or other Delphi components to hold
data, as they are easier to work with and quicker to program and debug.
There is much scope for error when programming using pointers!
Theory does indeed say that using pointers with large amounts of data should
lead to better performance (for the reasons above and more) but having said
that I haven't seen (or run) any recent benchmarks, and I suspect the
overhead of modern operating systems is far more significant than whether
you use a linked list or an array to hold data (say).
I hope that adds something to the discussion, and by the way, I don't always
say this but thanks to all for this forum - a most valuable source of
information.
Regards,
Brendan Blake.
(Dr W B J Blake)
email: [EMAIL PROTECTED]
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Gerry Bendixen
Sent: 15 March 2005 14:57
To: [email protected]
Subject: Pointer Usage
Hi all,
Recently the concept of using pointers has been discussed with some
favorable and some unfavorable comments. Not being an expert, I have
resorted to using pointers for one specific purpose and now I am questioning
whether my approach is optimum.
Here's the situation:
I have a 'main' unit that holds the main form with some components that I
want to be accessable by 'used' units. (TProgressBar, TEdit, etc.)
I declare a pointer to the component, assign a value in the main unit and
use the pointer in 'used' units to access the component.
My question is whether there are better concepts to achieve this
functionality.
Thanks in advance for any hints.
Gerry Bendixen
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi