这段代码可以在 Turbo C 上运行,但不能在 gcc 编译器上运行?

发布于 2024-12-13 19:47:12 字数 347 浏览 2 评论 0原文

此代码在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执笏见 2024-12-20 19:47:12

限定符 hugefarnear非标准。因此,虽然它们可能在 Turbo C 中工作,但您不能依赖它们在其他编译器(例如 gcc)中工作。

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).

2024-12-20 19:47:12

Borland 的 DOS C/C++ 编译器支持多种内存模型。

内存模型是一种通过指针访问代码和数据的方法。

由于 DOS 运行在所谓的 CPU 实模式下,其中通过成对的段值和偏移值来访问内存(每个通常是 16 位长),内存地址自然是 4 个字节长。

但段值不必总是明确指定。如果程序需要访问的所有内容都包含在一个段(在 16 字节边界上对齐的 64KB 内存块)中,则单个段值就足够了,一旦将其加载到 CPU 的段寄存器中( CS、SS、DS、ES),程序仅使用 16 位偏移量即可访问所有内容。顺便说一句,许多 .COM 类型的程序的工作原理与此完全相同,它们仅使用一个段。

因此,您有两种可能的方式来访问内存,使用显式段值或不使用显式段值。

在这些行中:

char huge *near *far *ptr1;
char near *far *huge *ptr2;
char far *huge *near *ptr3;

修饰符 farhugenear 指定 ptr1对象的邻近度>ptr2ptr3 将指向。它们告诉编译器 *ptr1*ptr2 对象将距离程序的主/当前段“很远”,也就是说,它们将位于一些其他段,因此需要通过 4 字节指针访问,并且 *ptr3 对象位于“附近”,位于程序自己的段内,并且 2 字节指针就足够了。

这解释了不同的指针大小。

根据您为程序编译选择的内存模型,函数和数据指针将默认为 nearfarhuge 以及除非您需要非默认指针,否则您不必明确地拼写它们。

程序内存模型为:

  • 微小:1 段用于所有内容;近指针
  • 小:1 个代码段,1 个数据/堆栈段;近指针
  • 介质:多个代码段,1 个数据/堆栈段;远代码指针,近数据指针
  • 紧凑:1个代码段,多个数据段;近代码指针,远数据指针
  • 大:多个代码和数据段;远指针
  • 巨大:多个代码和数据段;巨大指针

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 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:

  • tiny: 1 segment for everything; near pointers
  • small: 1 code segment, 1 data/stack segment; near pointers
  • medium: multiple code segments, 1 data/stack segment; far code pointers, near data pointers
  • compact: 1 code segment, multiple data segments; near code pointers, far data pointers
  • large: multiple code and data segments; far pointers
  • huge: multiple code and data segments; huge pointers

Huge pointers don't have certain limitations of far pointers, but are slower to operate with.

悲欢浪云 2024-12-20 19:47:12

您忘记在变量之间添加逗号:)。
如果变量的作用域相同,则它们不能具有相同的名称。

you forgot to put a comma between variables :).
Variables cannot have same name if their scope is same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文