Sunteți pe pagina 1din 2

********************** far , near & huge pointer *******************************

***
================================================================================
===
--> In DOS only 1 mb (10,48,580 bytes) of memory is accessible. Any of these mem
ory locations are accessed using CPU registers. Under DOS the CPU registers are
only 16 bits long. Therefore, the minimum value present in a CPU register could
be 0, and maximum 65,535, then how do we access memory locations beyond 65535th
byte?
--> By using two registers (segment and offset) in conjunction. For this the tot
al memory (1 mb) is divided into a number of units each comprising 65,536 (64 kb
) location, each such unit is called a segment. Each segment always begins at a
location number which is exactly divisible by 16.
--> The segment register contains the address where a segment begins, whereas th
e offset register contains the offset of the data/code from where the segment be
gins. For example, let us consider the first byte in B block of video memory.
The segment address of video memory is B0000h (20-bit address), whereas the offs
et value of the first byte in the upper 32K block of this segment is 8000h.
--> Since 8000h is a 16-bit address it can be easily placed in the offset regist
er, but how do we store the 20-bit address B0000h in a 16-bit segment register?
For this out of
B0000h only first four hex
--> digits (16 bits) are stored in segment register. We can afford to do this be
cause a segment address is always a multiple of 16 and hence always contains a 0
as the last digit. Therefore, the first byte in the upper 32K chunk of B block
of video memory is referred using segment:offset format as B000h:8000h.
Thus, the offset register works relative to segment register. Using both these,
we can point to a specific location anywhere in the 1 mb address space.
--> Suppose we want to write a character 'A' at location B000:8000. We must conv
ert this address into a form which C understands. This is done by simply writing
the segment and offset addresses side by side to obtain a 32 bit address.
--> In our example this address would be 0xB0008000. Now whether C would support
this 32 bit address or not depends upon the memory model in use. For example, i
f we are using a large data model (compact, large, huge) the above address is ac
ceptable.
--> This is because in these models all pointers to data are 32 bits long. As ag
ainst this, if we are using a small data model (tiny, small, medium) the above a
ddress won't work since in these models each pointer is 16 bits long.
--> What if we are working in small data model and still want to access the firs
t byte of the upper 32K chunk of B block of video memory?
-->In such cases both Microsoft C and Turbo C provide a keyword called f
ar.
--> normally Pointers are 32 bit length. which are divided as segment an
d offset.
which are represent as:
seg : off 0000 : 0000 . . . 8000:FFFF. . . . FFFF:FFFF

first 4 hexa digits are segment, last 4 hexa digits are offset
C Program will allocate 64KB (only one segment) memory for data Part (dy
namic memory allocation, Local variables).
by using 16 bit we can access that memory thats called near pointer(16 b
it pointer).
--> suppose we need more than 64KB memory to a program / we want to access a par
ticular memory location (in TSR Program) at the time we neet 32 bit pointer. thr
ough 32 bit pointer we can access any segment and offset address.
--> There are 2 types of 32 bit pointers::
1. far pointer.
2. Huge Pointer.
In TSR programming normally we use far Pointer.
The Main Difference between Far and Huge Pointer is
Far pointers are not Normalized.
Huge pointers are Normalized.
Different processors have different ways to handle memory, and also different am
ount of memory.
when you say "far" you are just telling the compiler find or put this data somew
here else.But the compiler does the hard work for you: writing the instructions
to change the segment properly and accessing the correct memory location for you
..and that's because it knows the processor..
DANGLING POINTERS
*****************
In C and C++, a pointer may be used to hold the address of dynamically allocated
memory. After this memory is freed with the free function (in C) or delete oper
ator (in C++) the pointer itself will still contain the address of the released
block. This is referred to as a dangling pointer. Using the pointer in this stat
e is a serious programming error. Pointers should be assigned 0 in C++, or null
in C after freeing memory to avoid this bug.