views:

600

answers:

13

Recently, I read a white paper by an individual who refers to a pointer to a struct as a handle. The author was clearly someone who had written C code on the windows platform previously. Googling indicates that windows programmers interact with system components via handles. I am wondering if it is common practice for windows programmers to refer to all struct pointers as handles? Or is the term handle meant to convey something beyond pointer to struct? I am asking as a linux C programmer.

The white paper I am referring to is: Duff, Heroux, and Pozo. An Overview of the Sparse Basic Linear Algebra Subprograms: The New Standard from the BLAS Technical Forum. ACM Transactions on Mathematical Software, Vol 28, No. 2, June 2002, Pages 239-267.

+3  A: 

Since you refer to handles being used as a pointer to a structure, as used by a Windows programmer, I'll answer within that context. Please note that there are clearly many different kinds of "handles", as it is a generic concept widely used within the computing environment. Certainly you will be familiar with the concept of a file handle; Windows also offers window handles and many other kinds of handles. Having said that:

A "memory handle" (that is similar to a pointer to a struct) is a concept from the land of 16-bit Windows programming, where there was no memory manager in the CPU and all memory management had to be done in software. Essentially, a "handle" was sort of a pointer, but the OS would be free to move around the memory that the handle referred to. You can't do that with a regular pointer, but the handle had functions that would get and release the actual memory address.

With the introduction of Win32, where the CPU had a hardware memory manager, the concept of the memory handle became obsolete. Other types of handles such as file handles and window handles still exist in Win32, but are not pointers to structs.

Greg Hewgill
Actually, a *handle* is a concept outside of Windows programming and is a reference to an object or thing. There are many design books that use the term *handle* without referring to Windows.
Thomas Matthews
My answer is limited to the context of the question which uses the phrase "refers to a pointer to a struct as a handle". Of course, there are many different kinds of handles, including file handles and window handles and whatever else. But none of those are pointers to structs.
Greg Hewgill
Any OS that manages resources that may or may not exist at a given memory location, or on which one wishes to prevent the call of functions such as delete or free() can benefit from the use of handles. It is not a Windows-specific thing, and is certainly not Win16 specific.
anon
I have updated my answer in light of the wider interpretation taken by commenters.
Greg Hewgill
+9  A: 

I use the word handle to mean a pointer that points to an "object" that represents a resource - often an OS resource, whereas a pointer just points to some memory. If you have a handle to something, you shouldn't try to read and write bytes into it directly, but manipulate it through provided methods.

Often handles are implemented as an opaque void *, which is further encouragement not to try to directly dereference it.

Mark Byers
+1 Handles are sometimes referred to as "opaque" handles for this reason.
Graeme Perrow
Actually, they are referred to as "opaque pointers"
anon
Heh, thanks - I added it in. Actually I didn't see your comment when I was adding it, but your comment was first anyway, so you get the credit! ;)
Mark Byers
Oops - I meant to say "Handles are sometimes referred to as "opaque" for this reason"
Graeme Perrow
A: 

A pointer is definitely different than a handle. A pointer is an address of something unspecified in memory. A pointer to a structure can be called a "handle" (usually by using 'typedef').

A handle is a concept used in writing the windows operating system. A pointer is a part of the C language.

Jay
I'd say a pointer to something unspecified is an OPAQUE handle, not just any handle.
kusma
A pointer in the C language has nothing whatsoever to do with a "handle" of any sort. It's an address used to access memory. What's in that memory may or may not happen to be what one programmer calls a "handle". You can within the language define what your expectations are when you use a specific pointer. Some programmers at microsoft made up a structure they called a handle and a typedef to reference it. I think it's important for setjmp to understand the difference. I think you're adding a lot of terminology that does nothing to help him understand a simple concept.
Jay
+20  A: 

The term handle generally means some opaque value that has meaning only to the API which produced it. In Win32, the HANDLE type is either a pointer in kernel memory (which applications cannot access anyway) or an index into some kernel-internal array.

Tim Sylvester
+1: Best answer I've seen so far.
Carl Smotricz
+1 More concise than mine :)
Mordachai
Great answer. For all us Posix people, a file descriptor is a good example of a handle.
R Samuel Klatchko
Actually on NT, the HANDLE type is a value which is contained in the per-process handle table which maps the handle value into a pointer in kernel memory, it's not an actual pointer value.
Larry Osterman
+2  A: 

The term handle is used to mean any technique that lets you access to another object. A handle can be a pointer, a reference, a pointer to a pointer, etc.. But sure its related to classes, objects etc. So handle need not always be a pointer to structure.

-AD.

goldenmean
+1  A: 

Windows defines handles for many things. They're not necessarily pointers at all -- some are, but others are things like offsets into particular tables. A few are intentionally obfuscated. There are handles for everything from windows to device contexts to bitmaps, and so on.

In any case, a handle is normally intended as an opaque data type -- i.e. you're not supposed to know anything about its value, only a set of predefined operations that can use it to accomplish various tasks. I believe C++/CLI also has a pointer-like object that's called a handle. I believe it's supposed to be closer to an opaque data type though -- if memory serves, you're not allowed to do any pointer arithmetic on them.

Jerry Coffin
+1  A: 

No, it is not particularly common amongst Windows programmers to refer to pointers as handles, but doing so isn't WRONG either. The term "handle" is usually used to describe something you use to access something through, and in that sense are all pointers handles (but not all handles are pointers). Win32's handles are AFAIK usually not pointers, but instead indices to internal OS tables - but this might change in future versions of Windows.

kusma
+1  A: 

Handles are generally pointers that you don't directly need to dereference. Rather you pass them to API calls which operate on the underlying structs.

Historically on Windows, handles were not pointers. You would lock the handle to get a pointer before using it, and unlock it when you were done (and the pointer would become invalid). In the days before paged memory, old-school Windows did it's own memory management by swapping out resources only referenced by handles and swap them back in when they got locked. In practice, this made memory management a nightmare, but allowed Windows to simulate virtual memory on systems without hardware support for it.

ataylor
+7  A: 

A handle is an old and revered concept.

A cookie is much the same thing. Or a GUID. Or a ticket to retrieve your car from a car park, or your coat from a fancy restaurant, etc.

Its any unique value that when presented back to the issuer can be used to track back to the actual thing referred, by whatever opaque mechanism the issuer wants. You may or may not know anything about that process, nor what the underlying thing is, exactly (only conceptually).

It was heavily used by Windows, but it is certainly not unique to Windows.

You would not normally use "handle" to mean "pointer to struct." Handle is more like "token" than like "pointer." It refers to something - file, system resource, memory, state-snapshot, etc. But what-exactly-it-is is based on the context of the handle itself (i.e. who issued the handle).

Handles were also used heavily in early filesystem programming in K&R C.

Mordachai
+2  A: 

In the old days of MacOS programming, back before OSX, a handle was a pointer to a pointer. That allowed the OS to move things around without invalidating the user's pointers. There were rules on when we could assume the pointed-to object wouldn't move, which I don't remember.

David Thornley
This definition coincides with the "opaque reference" usage suggested by others. The whole point of the second layer of indirection was that the OS would use the freedom afforded by this trick to optimize memory in the absence of a hardware supported memory abstraction layer. Basically the only times you could assume that the referenced memory would not move were 1) when *you* locked the memory and 2) when writing OS callbacks and other code that would run in kernel space.
dmckee
+2  A: 

The term "handle" didn't orignate on Windows, though it became widespread among Windows programmers.

In the C standard library (stdio.h), file handles are pointers to a data structure used by the C library.

Pure Unix programming uses file descriptors, which are indexes into a kernel data structure, but pointers have been used as handles in Unix for over 30 years.

Nick Dixon
+1  A: 

A handle is a generic term for a reference (not specifically a C++ reference) to an object.

A pointer is a subset of handle, since it points to objects.

A foreign key in a database is also a handle, since it points to records in other tables; and it is not a pointer.

In the Windows API environment, they used the abstract term handle so they could use an integer into a table, a pointer, or other methods, without interfering with the client; IOW, defining an abstract interface.

In summary, a handle can be something other than a pointer, such as an integer index or an object containing more details about the object (such as a smart pointer).

Thomas Matthews
+2  A: 

"Handle" is a logical term, not a physical one. It's meant as a proxy to a physical object to code that has more intimate knowledge of the object. A pointer to a struct is one such proxy, but there are many other possibilites.

Mark Ransom