near and far keywords inquiry

I noticed 2 keywords from a C code listings that I have seen. These are "near" and "far". They are used as shown in the c code snipets below

1
2
char far Flash_Page[PAGE_SIZE];
char near RamByte;


What are the keywords "near" and "far" for?
What does each statement in the above C Code Snipets mean? I know that they are a variable declaration of some sort but since it used the "near" and "far" keywords I dont know its complete meaning.

Thank you in advance.
near and far are nonstandard extentions from the world of 16 bit windows/dos.

a 'near' pointer was a 16-bit offset into a region of memory. The regions start address was implicit and dependant on the context.

'far' was a 32-bit pointer consisting of a 16-bit 'selector' that indirectly determined the
start address of the memory region, and a 16-bit offset into that region.
So does this mean that the Flash_Page is a 32-bit pointer while the near is a 16-bit pointer?

If so how do you dereference them? Do you still use the Asteris (*) before the indentifier to mean that its the contents of the mem loc that it is pointing to that you are interested in and not the memloc it self?

something like this?
1
2
*RamByte
*Flash_Page[1]


Thank you once again.
You can use them normally -- the only difference is when you declare them you are also declaring their storage requirements. For example, here's how to manipulate the console text color under DOS on an EGA or VGA card (by directly accessing the BIOS data area)
1
2
3
4
5
6
7
8
// EGA/VGA BIOS current text attribute
static unsigned char far *BIOS_current_color_attribute = MK_FP( 0, 0x466 );

unsigned char save = *BIOS_current_color_attribute;
*BIOS_current_color_attribute = 0x1E;  // yellow on dark blue
printf( "Hello world!" );
*BIOS_current_color_attribute = save;
printf( "\n" );


You only really need to worry about the difference in far and near pointers when compiling in tiny, small, or medium memory models -- in other modes all pointers are far by default (and introducing near pointers can be disasterous).

Hope this helps.
Last edited on
Just to make things clearer in my mind :)

Near is a pointer that requires a storage space of 16 bits (2 bytes) ?

wile

Far is a pointer that requires a storage space of 32 bits (4 bytes)?

No explicit declaration of a pointer as either far or near defaults it to far?

By using near or far pointer we are able to save storage space? Like if I am only to refer to a segment of memory and not all then I can just use near and not far right?

Actually I found these from a C Code Listing for a "ZiLog Z8 Encore XP Microcontroller". I was surprise to find these two as I have not encountered them before.

Thank you once again guys...
They're pointers for different memory models and as such have their own pointer arithmetic. It doesn't really make much sense without understanding the context in which they were used.

The Intel 16bit x86 architecture organised memory as overlapping 64k segments. If you wanted a pointer that was only used within a segment, you used a near pointer. If it needed to go beyond a segment, you needed a far pointer. The huge pointer was introduced later by Borland and was as large as a far pointer, but the arithmetic was done in such a way that you could compare them without having to worry about the segment:offset notation that made far pointers difficult to compare.

Intel 32bit x86 architecture still uses segments, but they're all 4G and all start at zero and so overlap, removing the need for the complicated segment:offset addressing scheme as pointers to different segments all take the same value.

The notion of memory model is a powerful one as it allows you an address range larger than your machine word size.

In C++, the STL's allocator was meant to encapsulate the memory model notion.
Topic archived. No new replies allowed.