加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – const记录参数是否放在堆栈中?

发布时间:2020-12-15 04:14:35 所属栏目:大数据 来源:网络整理
导读:假设我有一个名为TSomeRec的记录类型: type TSomeRec = record // ... end; …以及一个采用const TSomeRec参数的过程: procedure SomeProc(const someRec: TSomeRec);begin // ...end; 当调用SomeProc时,const someRec参数是作为值还是作为引用传递给堆栈
假设我有一个名为TSomeRec的记录类型:
type
  TSomeRec = record
    // ...
  end;

…以及一个采用const TSomeRec参数的过程:

procedure SomeProc(const someRec: TSomeRec);
begin
  // ...
end;

当调用SomeProc时,const someRec参数是作为值还是作为引用传递给堆栈的?

我问的原因是,在我正在处理的代码中,有问题的记录类型包含一个庞大的静态数组,因此是巨大的. (不,我无法改变它.)我很担心将这么大的记录放在堆栈上,我认为const可能会有所帮助.我遇到了这个代码溢出堆栈的问题,当发生这种情况时,我只会遇到神秘的访问冲突而不是任何类型的堆栈溢出错误.

解决方法

这取决于.这实际上是在在线帮助中描述的,但是这里有.

在32位Windows可执行文件(即用WIN32定义编译的代码)中,如果记录不大于32位寄存器,则按原样将其推入堆栈,即按值传递.如果记录大于寄存器,则传递指向记录的指针,即通过引用传递.这是默认的寄存器调用约定.对于其他平台或调用约定可能有所不同,但上述情况很可能是一般情况.

DocWiki:

Constant parameters may be passed to the function by value or by reference,depending on the specific compiler used. To force the compiler to pass a constant parameter by reference,you can use the [Ref] decorator with the const keyword.

But see the Delphi Language Guide too.它描述了如何传递所有类型的参数,例如

Sets,records,and static arrays of 1,2,or 4 bytes are passed as 8-bit,16-bit,and 32bit values. Larger sets,and static arrays are passed as 32-bit pointers to the value. An exception to this rule is that records are always passed directly on the stack under the cdecl,stdcall,and safecall conventions; the size of a record passed this way is rounded upward to the nearest double-word boundary.

不过,对于较新的编译器来说,这已经改变了.

但可以肯定的是,您可以随时查阅CPU视图.在那里你可以看到会发生什么.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读