This code run on Turbo C but not on gcc compiler
Error:syntax error before '*' token
#include<stdio.h>
int main()
{
char huge *near *far *ptr1;
char near *far *huge *ptr2;
char far *huge *near *ptr3;
printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
return 0;
}
Turbo C output is :4, 4 , 2
Can you explain the Output on Turbo C?
Borland's C/C++ compilers for DOS supported multiple memory models.
A memory model is a way to access code and data through pointers.
Since DOS runs in the so-called
real mode
of the CPU, in which memory is accessed through pairs of asegment value
and anoffset value
(each normally being 16-bit long), a memory address is naturally 4 bytes long.But segment values need not be always specified explicitly. If everything a program needs to access is contained within one
segment
(a 64KB block of memory aligned on a 16-byte boundary), a single segment value is enough and once it's loaded into the CPU's segment registers (CS, SS, DS, ES), the program can access everything by only using 16-bit offsets. Btw, many.COM
-type programs work exactly like that, they use only one segment.So, there you have 2 possible ways to access memory, with an explicit segment value or without.
In these lines:
the modifiers
far
,huge
andnear
specify the proximities of the objects thatptr1
,ptr2
andptr3
will point to. They tell the compiler that the*ptr1
and*ptr2
objects will be "far away" from the program's main/current segment(s), that is, they will be in some other segments, and therefore need to be accessed through 4-byte pointers, and the*ptr3
object is "near", within the program's own segment(s), and a 2-byte pointer is sufficient.This explains the different pointer sizes.
Depending on the memory model that you choose for your program to compile in, function and data pointers will default to either
near
orfar
orhuge
and spare you from spelling them out explicitly, unless you need non-default pointers.The program memory models are:
Huge
pointers don't have certain limitations offar
pointers, but are slower to operate with.