Reputation: 81841
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.
Upvotes: 18
Views: 6371
Reputation: 1106
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
Upvotes: 15
Reputation: 28616
A handle is an identifier to an abstract objects (bitmaps, sockets, files, memory, etc...) used to identify which object is affected by an operation: HBITMAP
, socket
, FILE*
are example of handles -- they are in fact integers possibly representing an index in some table or map in the operating system.
Strictly speaking, a pointer is a precise location in memory. While a pointer is sometimes used as a handle, the inverse is rarely (if ever) true.
In object oriented environments, handles are considered low level entities and it is a good practice to wrap handles in objects with the object having methods that calls the operations using the allocated handle. In that case, you have a reference to an object holding an handle, for instance: CBitmap
, System.Net.Sockets.Socket
, std::fstream
, etc.
Without delving into a language religious war, some will argue that one or the other is cleaner, safer, faster, easier. It almost always depend on what environment you are in -- you would be ill advised to use handles directly in C# if you have a choice, while in C it will be much simpler (and necessary) to use handles.
Important Note: In the .Net environement, if you ever have to make marshaling, you will end up reading something about the actual objects references being called handles. This is because they are in fact handles under the hood rather than pointers. So when you call a method on an object, the compiler is in reality calling the method with an handle to an object that can freely move in memory. This makes it possible for the garbage collector to avoid memory fragmentation. So with a System.Drawing.Bitmap
you end up with a Handle to a Pointer to a Handle.
EDIT:
Example, stdio/fstream in C++:
// Read a short line from a file and output it to the console
// 256 bytes max for simplicity purposes
char buffer[256];
// With handle
FILE* file = fopen ("file.txt" , "r");
fgets(buffer, 256 , file);
std::cout << buffer << std::endl;
fclose (file);
// With object reference
{
std::ifstream stream ("file.txt");
stream.getline(buffer, 256);
std::cout << buffer << std::endl;
}
The top example uses what should be considered a handle FILE*
to a file. The file
handle is allocated using fopen
and passed to operations such as fgets()
and close()
. close()
deallocate the handle.
The bottom example uses std::ifstream
. The handle is allocated in the object constructor and is internal to that object. To make operations on the file, you use the method provided by that object such as getline()
. The handle is deallocated by the stream destructor when the object goes out of scope, that is, at the closing bracket, or if the object was allocated on the heap, you would need to delete it explicitely.
Upvotes: 6
Reputation: 6850
A handle is just a piece of information that serves as a stand-in for a resource.
The most common example is a file handle, which is often nothing more than an integer that's assigned to each file you open so that you can distinguish them. They're used much more often in languages like C, where all the file. open()
returns an integer, and then in all of the other file access functions you need to pass that value as an argument in order to specify which file you mean. For example, in the function int read(int fd, void * ptr, int numbytes)
, you would pass it in as the argument for fd
.
C provides 3 standard file handles that should sound familiar:
In C#, handles are usually abstracted away as implementation details, and instead you're given an object to interact with. The best example of one that I can think of offhand is the (now deprecated) FileStream.Handle property:
public virtual IntPtr Handle { get; }
Upvotes: 7