这段代码可以在 Turbo C 上运行,但不能在 gcc 编译器上运行?
此代码在 Turbo C 上运行,但不能在 gcc 编译器上运行
错误:“*”标记之前的语法错误
#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 输出为:4, 4 , 2
您能解释一下 Turbo C 上的输出吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
限定符
huge
、far
和near
是非标准。因此,虽然它们可能在 Turbo C 中工作,但您不能依赖它们在其他编译器(例如 gcc)中工作。The qualifiers
huge
,far
andnear
, are non-standard. So, while they might work in Turbo C, you can't rely on them working in other compilers (such as gcc).Borland 的 DOS C/C++ 编译器支持多种内存模型。
内存模型是一种通过指针访问代码和数据的方法。
由于 DOS 运行在所谓的 CPU 实模式下,其中通过成对的段值和偏移值来访问内存(每个通常是 16 位长),内存地址自然是 4 个字节长。
但段值不必总是明确指定。如果程序需要访问的所有内容都包含在一个段(在 16 字节边界上对齐的 64KB 内存块)中,则单个段值就足够了,一旦将其加载到 CPU 的段寄存器中( CS、SS、DS、ES),程序仅使用 16 位偏移量即可访问所有内容。顺便说一句,许多
.COM
类型的程序的工作原理与此完全相同,它们仅使用一个段。因此,您有两种可能的方式来访问内存,使用显式段值或不使用显式段值。
在这些行中:
修饰符
far
、huge
和near
指定ptr1
、对象的邻近度>ptr2
和ptr3
将指向。它们告诉编译器*ptr1
和*ptr2
对象将距离程序的主/当前段“很远”,也就是说,它们将位于一些其他段,因此需要通过 4 字节指针访问,并且*ptr3
对象位于“附近”,位于程序自己的段内,并且 2 字节指针就足够了。这解释了不同的指针大小。
根据您为程序编译选择的内存模型,函数和数据指针将默认为
near
或far
或huge
以及除非您需要非默认指针,否则您不必明确地拼写它们。程序内存模型为:
Huge
指针没有far
指针的某些限制,但操作速度较慢。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.您忘记在变量之间添加逗号:)。
如果变量的作用域相同,则它们不能具有相同的名称。
you forgot to put a comma between variables :).
Variables cannot have same name if their scope is same.