From a question on the Practice C test from GeekInterview, why is the size of ptr1 2, while ptr2 and ptr3 are size of 4?
main()
{
char near * near *ptr1;
char near * far *ptr2;
char near * huge *ptr3;
printf("%d %d %d",sizeof(ptr1),sizeof(ptr2),sizeof(ptr3));
}
Output: 2 4 4
When working on architectures with segmented memory (like x86 real mode), one can distinguish three types of pointer addresses (examples for x86 in segment:offset notation):
near
Only stores the offset part (which is 16-bit) – when resolving such a pointer, the current data segment offset will be used as segment address.
far
Stores segment and offset address (16 bit each), thus defining an absolute physical address in memory.
huge
Same as far pointer, but can be normalized, i.e.
0000:FFFF + 1will be wrapped around appropriately to the next segment address.On modern OSes this doesn’t matter any more as the memory model is usually flat, using virtual memory instead of addressing physical memory directly (at least in ring 3 applications).