Pointers

Hello,

I know a little about pointers but I have yet to find a good use for them. When using them as string or integer variables, isn't it just as effective to use a regular variable? What are pointers used for? Can you show me an example?
spatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
hi spat,

pointers points to a (usally) allocated memory-area, which is need at runtime.

-- sample typed pointer
-- declaration

Type
  PPKRec = ^TPKRec;
  TPKRec = Record
             Id      : Integer;
             Kennung : String[2];
             KurzBez : String[12];
             Bez     : String[24];
             LangBez : String[48];
             PKId    : Integer;
             ProjId  : Integer;
           end;

var
  PPK : PPKRec;  //points to a TPKRec

-- sample usage
(Get and use Memory)

    New(PPK);  //allocate memory
    //Fill the memory with information
    PPK^.ID := qryPROBKAPITEL.FieldByName('PROBKAPITEL_ID').AsInteger;
    PPK^.Kennung := qryPROBKAPITEL.FieldByName('PROBKAPITEL_KENNUNG').AsString;
    PPK^.KurzBez := qryPROBKAPITEL.FieldByName('PROBKAPITEL_KURZBEZEICHNUNG').AsString;
    PPK^.Bez := qryPROBKAPITEL.FieldByName('PROBKAPITEL_BEZEICHNUNG').AsString;
    PPK^.LangBez := qryPROBKAPITEL.FieldByName('PROBKAPITEL_LANGBEZEICHNUNG').AsString;
    if qryPROBKAPITEL.FieldByName('PROBKAPITEL_PKAPITEL_ID').IsNull then
      PPK^.PKID := 0
    else
      PPK^.PKID := qryPROBKAPITEL.FieldByName('PROBKAPITEL_PKAPITEL_ID').AsInteger;
    PPK^.ProjID := qryPROBKAPITEL.FieldByName('PROBKAPITEL_PROJ_ID').AsInteger;
    //assign the pointer to the data-property of a treeview-Node
    locNodeKAPITEL.Data := PPK;

(free memory)

  PPK := Node.Data;  //get the Untyped Pointer from the Node
  If PPK <> Nil then
    Dispose(PPK);

--end sample typed pointer

you can allocate memory also with untyped pointers

--sample untyped pointer
--declaration
Var
  Ptr : Pointer

-- sample usage
(Get Memory)

  GetMem(Ptr,1000);  //allocate 1000 Bytes

Ptr points to the first Byte of the 1000

(free memory)
  FreeMem(Ptr,1000);  //free Memory

--end sample untyped pointer

one warning, don't forget to set the pointer to NIL after deallocate the memory, if you use the PointerVar for further Operations.

hope this helps a little

meikl
0
edeyCommented:
Also note that all object ref's are actually pointers so you call this:

my_bmp := TBitmap.create;

you are really setting a pointer(my_bmp) to a value that points to a bitmap created by the TBitmap.create method.  Pointers are also usefull for arrays whose dimensions are not known at design time.  Function/method pointers are also very common & very usefull, a prime example would be object event handlers.  Different objects can share the same handler simply because the handler called is pointed to, you got it, by a pointer.  In this way pointer's also allow classes to be more effecient by sharing code.  If you have 100 bmp's in your prog, there will not be 100 different declarations of TBitmap.dormant (for ex.) rather, when you call a bmp's dormant method the parameter list is expanded by one to include a pointer to the calling instance.  This way all the bmp's can use the same domant method.

Pointer's are a very necessary component of modern comp. sci.  They can be used in so many ways (many more then the few listed above) even if we don't also see them directly.

As for an example of a usefull pointer think of the following:

var
 my_array : array[0..50000] of integer;//~100k added to your program size
 PMy_array : PIntegerArray;//same memory requirements, but only 4bytes added;

init'ing PMy_array is easy to:

getMem(PMy_array,100000);
dispose(PMy_array);


tat's all there is to it.


GL
Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kretzschmarCommented:
hi edey(mike),
what means GL?
meikl
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

intheCommented:
^good_luck :-)
0
edeyCommented:
:)
0
synatureCommented:
In Delphi you generally will not ever need to use a pointer, Tobject will work much better, in terms of debugging and keeping you from all kinds of messy situations.

What pointers can do that variables cannot is live on the heap as opposed to the data segment of your program.  In other words, your .exe file contains space for each and every variable, but the .exe only contains an address (four bytes) for each pointer.

For example, an old sytle pascal string, ShortString, is a variable, and if you simple declare a shortstring without specifying length, you are declaring a 256 byte variable, and there will be a 256 bytes added to your .exe image to hold the value.  If you declare a string of the new default kind (starting with D2), the only space helf for it in the exe image is the four byte address (well, not entirely true, there is also a reference count and a length), but, unless you hard code storing data in it, that is all that will be in the .exe image.

In the "bad old days", before objects, any time you needed to work with dynamic data, data created or input during run time, you would use pointers to build linked lists, queues and so forth.  There are still some situations where the approach is a better one to take than objects, since the overhead is a lot less than objects.  But handling pointers is a sure fire way to learn about how easy it is to lock up your system!  If you think you need to use them, find an older Pascal text book that explains them in all their glorious and excrutiating detail.



0
kretzschmarCommented:
i get angry about you, synature,>:-(
please comment first,
and let the questioner decide!
0
spatAuthor Commented:
Thanks for your help guys.
0
kretzschmarCommented:
good choice, spat ;-)
0
synatureCommented:
Sorry guys, I didn't realize that points were so important.  A question was asked that I thought I could provide an answer for, didn't know everyone would get upset if I didn't call it a comment.
0
edeyCommented:
Hey, thanks spat :) :)
Normally I would like to get back a to you a little earlier, but I've been out of town since thurs morning.  Hope you all had a great weekend,


GL
MIke
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.