Sunday, July 12, 2009

What is null pointer, far and near ponters in c language?

A null pointer is one which doesnot refer to


any thing .





Far pointer : it refers to an address which not in the same


segment where pointer is defined.


Near pointer : refers to an adress in the same segment where


the pointer is defined

What is null pointer, far and near ponters in c language?
NULL pointer points to a place in memory that cannot be accessed.
Reply:In C/C++, NULL is generally represented with a 0. The NULL pointer points to nothing. So any public / externally accessible function or method that accepts a pointer should check for NULL. Example:





#define NULL 0


void printMe(const char * str)


{


if( str == NULL ) return;


printf("%s\n", str);


}








Expanding on the previous poster's answer:





Nowadays, the ideas of "near" and "far" pointers don't mean a thing unless you're writing embedded software on an old processor.





The notions of "near" and "far" pointers are remnants of Intel's segment:offset addressing on their 16-bit processors. A segment and offset were used to construct a complete address. The segment is shifted left 4 bits and the offset is added to come up with a 20 bit address (which limited addresses a total 1M addressable space). The offset referenced a 64K area of memory (the most that can be addressed in 16 bits).





An example of building a segment:offset address would be:





segment = 0x1234


offset = 0x2222


physical address =


(segment %26lt;%26lt; 4) + offset = 0x12340 + 0x02222 = 0x14562





Intel made it more efficient to access data within a 64K segment by including a register that defined the "current" segment. Near addressing required a 16-bit offset and was assumed to reside in the "current" segment. This would be a NEAR pointer. So NEAR pointers are only 16 bits.





If you need to access data outside the "current" 64K segment, you had to construct a segment:offset pair. This was a FAR pointer. A FAR pointer required 32 bits: a segment and an offset (even though the FAR pointer could still only access up to 24 bits, as defined by the segment:offset architecture).





One disadvantage is that it was inefficient to create data structures that were larger than 64K. Another disadvantage was that pointer arithmetic was a nightmare.





The "near" and "far" pointers started to disappear with the introduction of the 80386, Intel's first 32-bit processor. The '386 defined "protected" mode, where full 32-bit addresses could be used (DOOM ran in protected mode :)). However, to maintain compatibility with the vast amount of DOS software that was still around, the '386 retained segment:offset addressing.


No comments:

Post a Comment