What is the difference between a handle and a pointer?

Source: Internet
Author: User

The handle is different from the pointer, but the actual operations can be figured out. Create a form, or create DC, bitmap ...... Corresponding handles are generated. When these objects are destroyed, their corresponding handles become invalid. Or they can destroy these objects through their handles.

A handle is a pointer to a pointer. We know that a pointer is a memory address. After the application starts, the objects that make up the program stay in the memory. 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 needs 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 to specifically register the address changes in the memory of each application object, and this address (the location of the storage unit) it remains unchanged. After the Windows 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) → record 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 the original one, and the vast majority of cases are indeed different. If we regard going to a cinema to watch a movie as the startup and running of an application, the handles assigned by the system to the application are always different, this is the same as the fact that tickets sold to us at each cinema are always different.

HDC is the handle of the device description table.

CDC is a device description table class.

Use getsafehwnd and fromhandle to convert each other

Pointer can be used as a handle

Not all handles are pointers.

A handle only represents a number of an object.

Based on this number, you can find this object through a certain link.

Pointer is the address of an object

: Releasedc ();

Does this statement release the HDC handle or the memory referred to by the HDC handle?

DC is the context of the device. It is used as a pen shell that can be changed.

Is there only some objects with handles?

Int I;

Int * P = & I;

His memory won't change, right?

In short, the handle is a pointer to the structure.

: Releasedc ();

Int releasedc (

Hwnd, // handle to window

HDC // handle to device context

);

 

The application must call the releasedc function for each call to the getwindowdc function and for each call to the getdc function that retrieves a common device context

To obtain a reference to a DC, a system has a counter: getdc (); when the counter is + 1: releasedc (); when the counter is-1; otherwise, something cannot be correctly released.

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.