Near commands, long pointers, and giant pointers in C/C ++

Source: Internet
Author: User

C/C ++ Near commands, remote pointers, and giant pointers inSong Baohua Email: 21cnbao@21cn.com sweek in our C/C ++ learning career, in our brain impression, usually only pointer concept, I seldom heard that pointers are far, near, and giant. I have never heard of near pointers, far pointers, and huge pointers. Fliggy biography. Yes. One day of a month, you suddenly see the following statement: Char near * P;/* defines a character type "near" pointer */Char far * P; /* define a forward pointer */Char huge * P; /* define a struct type "Giant" pointer */I really don't know where the "near", "Far", and "huge" statements come from. What is the concept! This article tries to answer this question to relieve the confusion of many people. This must begin with the 8086 processor architecture and compilation sources. As you know, 8086 is a 16-bit processor. It has four segment registers dedicated to saving segment addresses: CS (code segment): code segment registers; DS (Data Segment ): data Segment register; SS (stack segment): Stack segment register; es (extra segment): additional segment register. 8086 when segment-based access is used to access data or instructions in this segment (within the 64 K range), you do not need to change the segment address (meaning the segment address register does not need to be modified ), when accessing data or commands outside the scope of this segment, you need to change the segment address (that is, the segment address register needs to be modified ). Therefore, in a 16-bit processor environment, if you access the address value in this segment, you can access it with a 16-bit pointer (indicating the intra-segment offset; to access the value of an address other than this segment, you need to use a 16-bit segment Offset + 16-bit segment address, a total of 32-bit pointers. In this way, we will know the difference between the far and near pointers: The near pointer is a 16-bit pointer that can only access this segment, only contain the offset of this segment; the remote pointer is a 32-bit pointer that can access non-local segments, including segment offsets and segment addresses. The near pointer can only access the addresses in 64 K bytes of data segments, such as char near * P; P = (char near *) 0 xFFFF; the far pointer is a 32-bit pointer, it indicates the segment address: the offset address. The remote pointer can be used for cross-segment addressing and can access the whole memory address. For example, if the remote pointer P points to the 0x2 address in 0x1000 segment, that is, 1000:0002, you can write: Char far * P; P = (char far *) 0x10000002; in addition to far pointers and near pointers, there is also a concept of giant pointers. Like a pointer, a giant pointer is also a 32-bit pointer, which also represents a 16-bit segment: 16-bit offset and can address any address. The difference between it and the far pointer is that it is normalized. The remote pointer is not normalized. Two remote pointers may actually point to the same physical address, but their segment addresses and offset addresses are different, such as 23b0: 0004 and 23a1: both 00f4 point to the same physical address 23604! The giant pointer ensures through a specific routine that the offset of each operation is less than 10 h, that is, only the minimum four digits have a value, and the other values are carried to the segment address, in this way, the far pointer can be unexpectedly bypassed at the 64 K boundary. Of course, an operation must be smaller than 64 KB. The following function converts a remote pointer to a giant pointer: void normalize (void far ** p) {* P = (void far *) (long) * P & 0xffff000f) + (long) * P & 0x0000fff00 <12);} from the above function, we can see the usage of pointer again. This function needs to modify the pointer value, therefore, the pointer passed to it must be used as a parameter. Here, I want to emphasize that near pointers, long pointers, and giant pointers are the products of 16-bit Block Addressing processors (if the processor is 16-bit but not segment addressing, there is no concept of near pointer, remote pointer, and giant pointer. The 32bit processor (more than 80386) currently used by normal PCs generally runs in protection mode, and the pointer is 32-bit, smooth address, no longer long, near pointer. However, in the field of embedded systems, 8086 of processors are still widely available, such as AMD's am186ed and am186er processors, we still need to figure out the addressing range of the pointer.

If you want to have a better understanding of the content described in this article, you may want to review the microcomputer principle, 8086 compilation, and refer to the relevant content of C/C ++ advanced programming books.

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.