如何重新创建一个检索对象最高元素的函数?

发布于 2024-08-05 20:29:00 字数 316 浏览 6 评论 0原文

我的计划是创建一个函数来检索对象的最高元素,也就是说,数组的上限。换句话说,我试图获取函数 High() 的代码。

到目前为止我已经尝试过:

 function High2(var X):integer;
 begin
   Result:=Pbyte(Cardinal(@X)-1)^-1;
 end;

上面的函数应该读取对象(数组/字符串)中第一个元素的位置之前的值(长度),并将其返回减少 1 的值。但是,它在静态上都不会检索正确的结果也不是动态数组类型。

如何在 Pascal 中重新创建 High() 函数?

My plan is to make a function that retrieves the highest element of an object, so to speak, the upper range of an array.In other words,I'm trying to get the the code of the function High().

What I have tried so far:

 function High2(var X):integer;
 begin
   Result:=Pbyte(Cardinal(@X)-1)^-1;
 end;

The function above should read the value(length) before the position of the first element in the object(array/string) and return it decreased by 1.However It doesn't retrieve correct results neither on static nor dynamic array type.

How do I recreate the High() function in Pascal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

各自安好 2024-08-12 20:29:00

High()(和 Low())函数被称为“标准函数”,这意味着它们是编译器固有的。与 Write 和 Writeln 一样,它们实际上并不作为普通 Pascal 函数声明而存在。编译器确保它们位于“系统”单元范围内只是为了方便,并允许在其他范围内使用相同名称的函数。这还允许您通过使用 System.High() 显式引用系统单元版本来限定它们。由于它们是内在的,编译器将自动为所考虑的类型生成正确的代码序列。这也意味着尝试复制它们的全部功能几乎是不可能的。只需坚持内在的标准功能即可。

The High() (and Low()) functions are referred to as "standard functions" which means they're intrinsic to the compiler. Like Write and Writeln, they don't really exist as normal Pascal function declarations. The compiler ensures that they are in the "System" unit scope merely as a convenience and to allow function of the same name within other scopes. This also allows you to qualify them by using System.High() to explicitly reference the System unit version. Since they are intrinsic, the compiler will automatically generate the proper code sequence for the type being considered. This also means that trying to duplicate the full functionality of them is nigh impossible. Just stick with the intrinsic standard functions.

梦巷 2024-08-12 20:29:00

不知道为什么当 Delphi 已经有一个内置的 High() 编译器魔法函数时你想要这样做,但是好吧,就这样吧。

静态数组:无法完成。运行时不会存储任何大小信息,因为编译器已知大小并且无法更改。 High() 只是将必要的数字作为常量放入代码中。

动态数组:编译器将 High 转换为对 System 单元中 DynArrayHigh 的调用,该调用返回 DynArrayLength - 1。DynArrayLength 从数组开头后退 4 个字节(仅后退 1)并将长度作为整数返回而不是一个字节。

希望这有帮助。顺便说一句,你为什么不直接使用高呢?

Not sure why you'd want to do that when Delphi already has a built-in High() compiler magic function, but OK, here goes.

Static arrays: Can't be done. No size information is stored at runtime since the size is known to the compiler and can't change. High() just drops the necessary number into the code as a constant.

Dynamic arrays: The compiler translates High to a call to DynArrayHigh in the System unit, which returns DynArrayLength - 1. DynArrayLength steps back 4 bytes from the start of the array (you're only stepping back 1) and returns the length as an integer instead of a byte.

Hope this is helpful. Why aren't you just using High, BTW?

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