What is the handle in c ++ ?, Handle is

Source: Internet
Author: User

What is the handle in c ++ ?, Handle is

This handle is only translated from the English handle. It is difficult to explain only what it means. This is the information I collect from other people's spaces.

Functional understanding:

What is "handle"? handle is intended to be a handle. It's what you deal with the operating system.

For example, if you are admitted to a university, the school (operating system) will give you a student ID card number.

Note that this number is specified by the school and you cannot select it.

With this number (student ID card, if one card is used for multiple purposes), you can enjoy the services provided by the school:

For example, you can borrow books from the library, eat in the canteen, and attend classes in the classroom.

But you cannot buy beer in the canteen because the school does not allow this service.

In computer systems, the service provided by the system is API calling. With HANDLE, you can confidently propose the service to the system to call the API.

The pointer has a lot of power. With the pointer, you can drink and fight everywhere, and the school (operating system) cannot handle it,

Therefore, the difference between a handle and a pointer is that the handle pointer calls the services provided by the system.

While the handle is a number that can be differentiated from each other, it is different from our ordinary ID number,

A normal ID number can be defined by the programmer, but cannot be a handle. It is specified by the system for object generation,

This is to differentiate the objects in the system. This handle is not given by the programmer.

Conceptual understanding

1. A handle is the basis of windows programming. A handle is a unique integer,

A four-byte value is used to mark different objects in an application and different instances of similar objects,

For example, a window, button, icon, scroll bar, output device, control, or file.

The application can access the information of the corresponding object through a handle.

2. A handle is not a pointer and cannot be used by a program to directly read information in the file.

It is useless if the handle does not need to be in an I/O file.

3. A handle is a unique integer that windows uses to indicate or use in an application,

Windows uses a large number of handles to mark many objects.

Mechanism understanding

The previous analysis is classic, but I think there is something that must be pointed out. If not, please testify.

The handle is a pointer, not false at all, but this pointer is different from the pointer in C.

Because Windows is a multitasking system, its memory can be moved,

In this case, if a pointer points to a piece of memory at a certain moment, it will be removed by the system at a certain time,

If you use this pointer again, an error will occur.

To solve this problem, windows opens a memory block in the system area to store the handle. The value of this handle is an address,

After the memory is removed, windows modifies the value of the handle and then accesses the memory. The value of the handle is always valid.

This is because when you use the memory allocated by GlobalAlloc, If you specify the attribute of this memory to be fixed,

Then its return value can be directly given a pointer, if it can be moved,

The returned value must be a handle. You must use GlobalLock before using it.

I understand the handle, right?

My understanding

Actually, a handle is a pointer to a pointer. That is:

In windows programming, a handle is only a number used by an application to identify something.

To better understand the handle, I can tell you that the handle is a pointer to the pointer.

We know that a pointer is a memory address.

After the application is started, the objects that make up the program are stored.

If we simply understand it, it seems that we only need to know the first address of the memory, then we can use this address to access the object at any time.

However, if you really think so, you are very wrong.

We know that Windows is an operating system based on virtual memory. In this system environment,

Windows Memory Manager often moves objects back and forth in memory to meet the memory requirements of various applications.

The object is moved, which means its address has changed. If the address is always changing, where should we find this object?

To solve this problem, the Windows operating system frees up some internal storage addresses for each application,

It is used to register the address changes of each application object in the memory, and the address (location of the storage unit) itself remains unchanged.

After the Wi ndows Memory Manager moves the object's location in the memory, it notifies the new address of the object to save the address.

In this way, we only need to remember this handle address to indirectly know the location of the object in the memory.

This address is assigned by the system during object loading. When the system is detached, it is released to the system.

Handle address (stable) → recording the address of the object in the memory → address of the object in the memory (unstable) → actual object

However, it must be noted that each time the program is restarted, the system cannot ensure that the handle assigned to the program is still

The original handle, and the vast majority of cases are indeed different. Let's assume that we watch a movie in a cinema

Is the startup and running of an application, the handle assigned by the system to the application is always different.

The tickets sold by cinemas to us are always different.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.