# Far pointer

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Far_pointer
> Markdown URL: https://mediated.wiki/source/Far_pointer.md
> Source: https://en.wikipedia.org/wiki/Far_pointer
> Source revision: 1346578818
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{short description |Address to memory outside immediate vicinity}}
{{Use American English |date=February 2024}}
{{Use mdy dates |date=February 2024}}
{{More citations needed |date=February 2024}}
In a [segmented architecture](/source/memory_segment) computer, a '''far pointer''' is a [pointer](/source/pointer_(computer_programming)) to memory in a specific context,<ref name="mine23" /> such as a [segment selector](/source/Memory_segment) making it possible to point to addresses outside of the default segment.

Comparison and arithmetic on far pointers is problematic: there can be several different segment-offset address pairs pointing to one  [physical address](/source/physical_address).

== In 16-bit x86 ==
{{further|x86 memory models}}
For example, in an [Intel 8086](/source/Intel_8086), as well as in later processors running [16-bit](/source/16-bit) code, a far pointer has two parts: a 16-bit [segment value](/source/x86_memory_segmentation), and a 16-bit [offset](/source/offset_(computer_science)) value. A linear address is obtained by shifting the binary segment value four times to the left, and then adding the offset value. Hence the effective address is 21&nbsp;bits<ref group=Note name=technicality/>. There can be up to 4096 different segment-offset address pairs pointing to one physical address. To compare two far pointers, they must first be normalized to a form with only one representation address.  Such a normalized form may be one that minimizes the segment (maximizing the offset), minimizes the offset (maximizing the segment), or converts the 2-part segmented address to a (20-bit) linear representation.  One commonly used normalized form minimizes the offset part of the address to a value less than 16 (10 hex): such a normalization can be computed simply by taking the low-order 4 bits of the unnormalized offset as the new offset, and adding to the unnormalized segment the unnormalized offset shifted right by 4 bits.

On [C](/source/C_(programming_language)) [compiler](/source/compiler)s targeting the 8086 processor family, far pointers were declared using a non-standard {{code |lang=C |far}} qualifier; e.g., {{code |lang=C |char far *p;}} defined a far pointer to a [char](/source/Character_(computing)). The difficulty of normalizing far pointers could be avoided with the non-standard {{code |lang=C |huge}} qualifier. On other compilers it was done using an equally non-standard {{code |lang=C |__far}} qualifier.<ref name="wat24" />

Example of far pointer:
<syntaxhighlight lang="c">
#include <stdio.h>

int main() {
    char far *p = (char far *)0x55550005;
    char far *q = (char far *)0x53332225;
    *p = 80;
    (*p)++;
    printf("%d", *q);

    return 0;
}
</syntaxhighlight>
:Output of the following program: 81; Because both addresses point to same location.
:Physical Address = (value of segment register) * 0x10 + (value of offset). 
:Location pointed to by pointer {{code |lang=C |p}} is :  0x5555 * 0x10 + 0x0005 = 0x55555
:Location pointed to by pointer {{code |lang=C |q}} is :  0x5333 * 0x10 + 0x2225 = 0x55555
:So, {{code |lang=C |p}} and {{code |lang=C |q}} both point to the same location 0x55555.

== In ESA/390 and z/Architecture ==
On some C compilers for [ESA/390](/source/ESA%2F390) and [z/Architecture](/source/z%2FArchitecture), far pointers can be used to include an identifier of an alternate address space to access.<ref>{{cite book |chapter-url=https://support.sas.com/documentation/onlinedoc/sasc/doc750/html/clug/z1824605.htm |chapter=Access Register Mode Support |title=SAS/C Compiler and Library User's Guide, Release 7.00 |publisher=[SAS](/source/SAS_Institute)}}</ref><ref>{{cite web |publisher=IBM |title=Metal C Programming Guide and  Reference |pages=35|url=https://www.ibm.com/docs/en/SSLTBW_2.1.0/pdf/ccr1u201.pdf}}</ref>  [ESA/370](/source/ESA%2F370), ESA/390, and z/Architecture include facilities for a program running in one address space to access data in another via "access registers".

== On AVR ==
On larger [AVR microcontrollers](/source/AVR_microcontrollers), addressing more than 64&nbsp;KB is possible via far pointers, which include an identification of the 64&nbsp;KB "page" to access.

==Notes==
{{reflist|group=Note|refs=
<ref name=technicality>Early x86 processors only had a 20-bit address bus so results above 1MiB [wrapped around](/source/Integer_overflow) to zero, discarding the carry bit. PC's using the 80286 or newer, which had the necessary address lines, implemented the [A20 gate](/source/A20_line) to toggle this behavior for [backwards compatibility](/source/Backward_compatibility) with older software.</ref>
}}

==References==
<references>

<ref name="mine23">{{cite Q |Q1120519 |title=Pointers in Far Memory |first1=Ethan L. |last1=Miller |first2=George |last2=Neville-Neil |first3=Achilles |last3=Benetopoulos |first4=Pankaj |last4=Mehra |first5=Daniel |last5=Bittman |date=December 2023 |journal=[Communications of the ACM](/source/Communications_of_the_ACM) |volume=66 |number=12 |url= 
https://m-cacm.acm.org/magazines/2023/12/278153-pointers-in-far-memory/fulltext |access-date=2024-02-11 }}</ref>

<ref name="wat24">{{cite web |title=Introduction to Open Watcom C/C++ |date=2024 |website=GitHub |url=https://open-watcom.github.io/open-watcom-v2-wikidocs/c_readme.html |access-date=2024-02-11 }}</ref>

</references>

{{DEFAULTSORT:Far Pointer}}
Category:Computer memory
Category:Pointers (computer programming)

---
Adapted from the Wikipedia article [Far pointer](https://en.wikipedia.org/wiki/Far_pointer) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Far_pointer?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
