Integer value Pointer

Ok this is interesting Question.

If you have noticed every component have property "TAG" and its integer. Not many components have property pointer. For exsample every TTreeNode has propery "Data" wich is poiner its value is nil and i can use this pointer for my extensions, like adding some class or so.

So what is my idea.

pointer value is integer only in hex decimal. So for better view i will show my idea

I creare TList.


I asign T pointer value to a pointer P


For my component i have "TAG" propery I want to add pointer valu to "TAG" converting pointer in integer value

it should look like so


i:=pointertoint(p);//i know pointertoint function doesn't exist :) but that is the idea i wan't to get pointer value to integer and beck from integer value to pointer :).


So why i'm so interested is this kind of taging :) if component doesn't have pointer poperty i can use "tag" property like pointer to some object.

But i need these to function:


I think this idea is nice i will continue to solve this interesting idea my self but fieel free to try to solve it first.
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.

Russell LibbySoftware Engineer, Advisory Commented:
Because pointer and integer are both 4 byte data types, a simple cast can be used.

var i: Integer;
     p: Pointer;

 i:=Integer(p); // Cast pointer to int
 p:=Pointer(i); // Cast int to pointer





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
Yep, Russel is spot on ...



Stores an integer value as part of a component.

property Tag: Longint;


Tag has no predefined meaning. The Tag property is provided for the convenience of developers. It can be used for storing an additional integer value or it can be typecast to any 32-bit value such as a component reference or a pointer.

You can freely type cast any 32-bit variable (or less) to another 32-bit type.
32 bit type in Delphi are: pointer, integer, cardinal, single.

So if you want to 'save' a pointer value to the 'tag' property you can do that:

Button1.Tag := Integer(Button2);

which.. later on, you can do this:


Or.... the other way around, you can create a list of integer values using TList:

var I: Integer;
MyList.Add(TObject(I)); // type cast integer to pointer type

... and retrieve it again:

I := Integer(MyList.Items[0]);

Note that the compiler will give you warning of unsafe typecasting, you'll need
to be very carefull with this method.

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.

Or... of course you can create your 2 conversions functions:

function PointerToInt(APointer: Pointer): Integer;
  Result := Integer(APointer);

function IntToPointer(AInt: Integer): Pointer;
  Result := Pointer(AInt);

TheRealLokiSenior DeveloperCommented:
here's a working example from 1 of my projects

type TPositionObject = class
    startx, starty: integer;

panel2.Tag := Integer(TPositionObject.Create);

procedure TForm1.Panel_MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
        TPositionObject( (Sender AS TPanel).Tag).startx := X;
        TPositionObject( (Sender AS TPanel).Tag).starty := Y;
        TPositionObject( (Sender AS TPanel).Tag).Moving := True;

Kyle FosterCEOCommented:
Keep in mind that Pointer and Longint while in 32 bit windows are the same size.  That is not guaranteed in 64 bit windows.  Not sure how Delphi and Borland will/ or do handle this in 64 bit.
Also keep in mind that -some- exisiting 3rd party component sets use this for there own purposes, commonly, internationalisation and skinning components, for instance.
I created this little experiment to see what will happen.  I learned something today.

procedure TForm1.Button1Click(Sender: TObject);
  _pointers : TList;
  x, iTestP, iTestV ,i: integer;
  lb, lsTest : TListBox;
  x := 5;
  lb := ListBox1; // A listbox on my form
  lb.Items.Add('Line 1');
  lb.Items.Add('Line 2');

  _pointers := TList.Create;
  //You need to add the @ before type variables to get a pointer to that value.
  _pointers.Add(lb);//Objects are always pointer.

  lsTest := TListBox(_pointers.Items[1]);//Retreive listbox pointer from IList
  iTestP := integer(_pointers.Items[0]);//This returns a pointer to integer value
  iTestV := integer(_pointers.Items[0]^);//Returns the value of the integer

  //iTestP is a pointer to my origional value.  You can also retreive that
  //value by
  iTestV := integer(pointer(iTestP)^);

  //This means you can store the pointer (iTestP) in the tag element, and
  //later on retreive the value like just above.

  ShowMessage('Pointer to origional value ' + IntToStr(iTestP));
  ShowMessage('Origional value ' + IntToStr(iTestV));

  for i := 0 to lsTest.Items.Count-1 do begin
Kyle FosterCEOCommented:
ruanlab123 just for clarification in case someone reading your post doesn't understand it.

you declared your x,iTestP,iTestv as Integers in the local context and got the address to those pointers with the @ operator.  These pointers are not valid outside this proc's scope.

try this with your test.

x :PInteger;  or X: ^Integer;  (same thing).

now x is a true pointer type.

New(X) will get the memory for an Integer and X will POINT to that memory (or in English the value of X is the @ of the memory returned from the New function).

While the pointer X is local to the scope of your function.  The memory that X POINTS to is not.  So if you add x to a TList that is global to an object then it can be accessed by any code that has access to the object.  ex.  Tlist.Get accesses a list of pointers that are global to the TList object.

When you wish to access the value that X points to you use the^.

    MyInt : Integer
   MyInt := X^;  not X;

also You obtained the memory with the NEW function.  It is your responsibility to release it with the DISPOSE function.


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

From novice to tech pro — start learning today.