Reputation: 85
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?
Upvotes: 1
Views: 1608
Reputation: 62048
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 a segment value
and an offset 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:
char huge *near *far *ptr1;
char near *far *huge *ptr2;
char far *huge *near *ptr3;
the modifiers far
, huge
and near
specify the proximities of the objects that ptr1
, ptr2
and ptr3
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
or far
or huge
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 of far
pointers, but are slower to operate with.
Upvotes: 5
Reputation: 4165
you forgot to put a comma between variables :). Variables cannot have same name if their scope is same.
Upvotes: 1
Reputation: 182639
The qualifiers huge
, far
and near
, are non-standard. So, while they might work in Turbo C, you can't rely on them working in other compilers (such as gcc).
Upvotes: 6