What is an object's handle?
The genre term is generally used to refer to the method of acquiring another object - a generalized fake pointer. This term is (deliberate) ambiguous.
Very vaguely clear is useful in some cases of actual. For example, in an early design, you may not be ready to use your handle. You may not be sure if a simple pointer or reference or pointing to a pointer or a pointer pointing to a reference or integer identifier is placed in an array or string (or other key) to enable a hash-table. (Or other data structure) or database keys or some other techniques to query. If you only know that you will need some unique identity to get an object, then these things are called handles.
Therefore, if your ultimate goal is to let the code unique identity / query the specified object of a Fred class, you need to pass a Fred handle to these code. The handle can be a string that can be used as a key (key) as a well-known query table (for example, in std :: map
Beginners often consider pointers, but actually use uninited pointers with the bottom level. For example, what if the Fred object needs to be moved? How do we know when the Fred object can be securely deleted? What should I do if the Fred object needs (temporary) continuous from the disk? and many more. Most of these times, we add an indirect layer to manage the location. For example, the handle can be fred **, pointing to the pointer to the fred * guarantee that it will not be moved. When the Fred object needs to move, you can update the pointer pointing to Fred *. Or use an integer as a handle, then query the Fred object (or pointing to the fly object) in the table or array or other part.
The focus is when we don't know the details of what you want to do, use your handle.
Another timing using the handle is to "sometimes using the term Magic Cookie, just like this," software is transmitted, "software is transmitted," "Software passes a Magic cookie to uniquely identify and locate the appropriate Fred object"). The reason why the already completed thing is to minimize the special details of the handle or when the representation is changed. For example, we don't want to update a large number of code when you get a handle to query the integer in the array from the string used in the table.
When the details or representations of the handle changes, the maintenance work is simpler (or easier reading and writing code), so it is often encapsulated in the class. Such types often overload Operator-> and Operator * operators
(Since the effect of the handle is like a pointer, then it may look like a pointer).
Difference between memory handle and pointer
The pointer to the address of a data in memory, it is possible to freely modify the data. Windows does not want the general program to modify its internal data structure because it is too unsafe. So Windows specifies a handle to each memory area that is declared using a function of GlobalAlloc (nature is still a pointer, but do not manipulate it), usually you just use this handle when calling the API function. When you need to perform direct operations for a memory, you can use Globalock to lock this memory and get a pointer to do it. Comments 1: The handle is a "pointer" of the pointer, and the handle is mainly to facilitate Windows to move allocated in the process memory address space to prevent the process's memory space from being torn four-fold five-fold-five-fold cuts. Opinions 2: The handle is a pointer to the pointer to the index of some tables. Indirect reference object, Windows can modify the "physical" address of the object and the value of the descriptor, but the value of the handle is constant. Opinions 3: The handle and pointer are the address, the different lies in: 1, the handle can be a very complex structure, and it can be related to the system, such as the handle of the thread mentioned above, it points to It is a very type of or structure, and he has a very close relationship with the system. When a thread is unpredictable, the system can return to the information it occupied by the system, such as CPU, memory, etc., in turn I know that some items in this handle are interacting with the system. Due to the Windows system, it is a multi-task system that may allocate memory, recycle memory, and restructuring memory. 2, the pointer can also point to a complex structure, but is usually user-defined, so the required work must be done, especially when deleting. However, there are also some pointers in VC 6.0, which are handled for some small problems, such as pointers such as the most common characters, which also wants to handle memory, but cstring does not handle users, It is actually a class in VC , so the operation is completed by the member function, generating (allocation) is completed by the constructor, deleting (recycling) by the destructor. Q: Hello, I have encountered a lot of handles in learning multithreaded multithreading with VC 6.0, but I don't understand his specific role and how to use the handle, I hope you can give me a few specific instances, not very grateful! For example: Handle Hthread, how is it specifically used? A: After you use CreateThead, the function returns a handle that represents this thread. You may call setthreadpriority to modify the thread's priority, use ResumeTHRead to restart a thread run, you need to tell the system which thread you want to do, and you just returned, these The first parameter of the function is the handle of the thread. What is the difference between handle and instance?