This topic is now archived and is closed to further replies.


what is a handle?

Recommended Posts

PPCThug    130
from what I''ve read a handle sounds a lot like a pointer, so there should be a good reason for a different name.
the Particle Projection Cannon fires a shimmering blue bolt, much like a cross between lightning and a sine wave that ripples along its path.

Share this post

Link to post
Share on other sites
Vaporisator    122
Imagine you have a big warehouse (your memory) with many cells where you can put packets (the actual information).

Now a pointer would tell you in which cell the packet is it is pointing to. But if you now for some reason move the packet to another cell, the pointer is still pointing to the old cell where the packet has been. So it now points to the old packet.

A handle is something like a packet-id, so you can still find your packet, even when it has been moved to another cell.

Or to take another example:
How to indentify a Person

Pointer: He lives in 5th Park Avenue, New York, USA

Handle: He is called Micheal Jackson (assuming there is only one)

So, you see, if Micheal moves to another house, you cannot find him any longer by the pointer, but the handle would know his name, so you can look up his current adress in a telephone book.

Hope this is correct and understandable.

Yesterday we still stood at the verge of the abyss,
today we''re a step onward!

Share this post

Link to post
Share on other sites
Landsknecht    234
Also, most of the time (at least in Windows land...) a handle is nothing more than a typedef used on a primitive data type (like an int). The handle is a box location and as windows shuffles stuff around - it updates the pointers in the boxes. You give windows the box number (handle) it goes and gets the pointer...

Happens transparently for the most part.


Share this post

Link to post
Share on other sites
Steveyboy    122
What i use handles for:
class CSoundManager
// ...

HANDLE CreateSample();

CSample* m_pSamples[32]; // My array of sound sample pointers

int m_nNextEntry;

HANDLE CSoundManager::CreateSample()
CSample* pSample = new CSample();
m_pSamples[m_nNextEntry] = pSample;
return (HANDLE)(m_nNextEntry);

So i give the user a HANDLE, not a pointer or an int. If i use a pointer, the sample might be deleted, and the user would still have a pointer to duff memory. If i use an int, it looks a little more unproffesional (the user will be able to see its an array index).
HANDLEs tend to be used instad of a void*, as array indices, or an offset into a reserved memory block (take a look at WinSock SOCKETs).

HTH, Steve

Share this post

Link to post
Share on other sites
Siebharinn    122
A HANDLE is a way to provide a reference to a piece of data, without providing the data. For instance, if you call:
FILE* fp = fopen(...)
you can conceivably poke around inside the FILE structure. With a handle, you can't. The handle is just a reference to the data, which is stored else where. Now as some have mentioned, many times a handle is just a pointer, or a pointer to a pointer. It could just as easily be an index into an array or some other means to retrieve a specific piece of data.

Back in the old days, when intel processors used segmented memory, handles were a way to get around segment limitations. Rather than just allocating all the memory you'd need and holding the pointers (which would be invalidated when the segment pointer changed), you'd allocate the memory and receive a handle. When you needed to actually access that memory, you'd call a lock function which would return a pointer to the real memory location. When you were done, you'd unlock it. This way Windows could rearrange the memory without invalidating all your pointers.

Take care,

EDIT - Pointless quote

Edited by - Siebharinn on February 8, 2002 5:03:52 PM

Edited by - Siebharinn on February 8, 2002 5:05:11 PM

Share this post

Link to post
Share on other sites