Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a Pointer and a Handle

I cannot say I have a strong C++ background so I hear Handle very often these days. I know what a Pointer is (which stores address of the memory location like reference) but I am not really sure what Handle is and what the differences are between these two. And a sample code in C# would be great if you can provide one.

By the way I googled this terms however many people gave different explanations so I think I get the best one from SO.

EDIT:

A quick heads-up for the other visitors: A handle is like a reference which points a resource. It can point at a memory address like a Pointer but Handle is more general term so it is more like a pseudo-pointer. A file is a good example for this. A file can have an ID which OS can understand and use to find the file. So Handle can hold this ID (which may or may not be a Memory Address) and when we pass this Handle in, OS can find the file easily.

Please refer to answers below for more detailed information.

Edit:

All the answers under this question are awesome and very explanatory. I am having a hard time to select one of them to mark it as answer.

like image 342
Tarik Avatar asked May 22 '12 14:05

Tarik


1 Answers

Sorry, no C# example, but:

Pointer is a memory address, which in this case points to where the object is stored in memory. This is a low level concept that C++ inherited from C.

Regarding the handle:

The term handle is used to mean any technique that lets you get to another object — a generalized pseudo-pointer. The term is (intentionally) ambiguous and vague.

One more important term that is closely related is an object reference, which is an "alias" for the object.

You may get a rather clear and concise answers on this page

like image 66
Alex Kreimer Avatar answered Sep 18 '22 15:09

Alex Kreimer